【问题标题】:Android weighted fragment not showingAndroid加权片段未显示
【发布时间】:2016-04-20 14:17:09
【问题描述】:

基本上我想要做的是让屏幕的一半是选项卡布局和一半图像(或其他任何东西)。在我开始增加体重之前它工作得很好。

照原样,没有显示任何内容。什么会导致这个问题?

<android.support.v4.app.FragmentTabHost
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:gravity="left"
    android:weightSum="2"
    android:background="#00025a">

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:layout_weight="1">
        <TabWidget
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@android:id/tabs">
        </TabWidget>

        <FrameLayout
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:id="@android:id/tabcontent">
        </FrameLayout>

        <FrameLayout
            android:id="@+id/tabcontent"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
        </FrameLayout>
    </LinearLayout>

    <ImageView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:src="@drawable/test_room_1"/>

</android.support.v4.app.FragmentTabHost>

如果没有权重组件,选项卡相关的内容会显示,但我假设它会将图像推出,因为它没有显示。

任何帮助将不胜感激。 :)

更新

这里是按要求提供的屏幕截图:Weighted

以及调用活动:

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTabHost;
import android.widget.ImageView;

public class sandbox_mode extends FragmentActivity {
    ImageView level_background;

    private FragmentTabHost mTabHost;

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sandbox_mode_play);

        mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
        mTabHost.setup(this, getSupportFragmentManager(), R.id.tabcontent);

        mTabHost.addTab(mTabHost.newTabSpec("Enemy Tab").setIndicator("Enemies"),
                enemy_tab.class, null);
    }
}

【问题讨论】:

  • 可以加截图吗?
  • 你如何称呼这个片段。请添加该代码。

标签: java android android-fragments android-tabhost


【解决方案1】:

如果你查看FragmentTabHostlink 上的文档,你会发现它不是从LinearLayout 继承的,所以这个容器基本上不支持 layout_weight。我建议添加额外的顶部 LinearLayout 以使其正常工作。

<android.support.v4.app.FragmentTabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:gravity="left"
android:weightSum="2"
android:background="#00025a">
<LinearLayout
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="horizontal"
   android:weightSum="2" />

    <LinearLayout
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:layout_weight="1">
    <TabWidget
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@android:id/tabs">
    </TabWidget>

    <FrameLayout
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:id="@android:id/tabcontent">
    </FrameLayout>

    <FrameLayout
        android:id="@+id/tabcontent"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    </FrameLayout>
    </LinearLayout>

    <ImageView
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:src="@drawable/test_room_1"/>
</LinearLayout>

</android.support.v4.app.FragmentTabHost>

【讨论】:

  • 谢谢!这为我做到了。回到我不明白的更多android垃圾中。
【解决方案2】:

使用 childFragmentManager 代替 FragmentManager

mTabHost.setup(getActivity(), getChildFragmentManager(), R.id.tabcontent);

如果您想在原生片段中使用嵌套片段。使用getFragmentManager()

如果您想在支持库片段中使用嵌套片段,请使用getChildFragmentManager()

【讨论】:

  • 是否需要额外的包含才能使用 'getChildFragmentManager()'?
  • 我已经看过了。这就是我能走到这一步的主要原因。
  • getChildFragmentManager() 在这种情况下对我来说不是一个东西。特别是“无法解析方法'getChildFragmentManager()”
  • 所以你从上面提到的链接中检查的内容。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-25
  • 2017-09-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多