Android加权片段没有显示

时间:2016-04-20 05:04:09

标签: java android android-fragments android-tabhost

基本上我要做的就是让屏幕的一半是标签布局和半个图像(或其他任何东西)。它工作得很好,直到我开始增加重量。

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

<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);
    }
}

2 个答案:

答案 0 :(得分:0)

使用 childFragmentManager 代替FragmentManager

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

如果要在本机片段中使用嵌套片段。使用getFragmentManager()

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

答案 1 :(得分:0)

如果您查看FragmentTabHost link上的文档,您会发现它不是从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>