android - 选项卡位于 xml 中文本的顶部

标签 android xml android-fragments android-tabhost

我正在使用 android TabHost 来向我的 android Fragment 添加选项卡。我的问题是选项卡位于我的 xml 之上。我认为我的 fragment xml 会自动在选项卡下启动?

enter image description here

我的这些选项卡的代码是:

public class StatisticsTab extends Fragment {


    private FragmentTabHost mTabHost;

    //Mandatory Constructor
    public StatisticsTab() {
    }

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_tabs,container, false);


        mTabHost = (FragmentTabHost)rootView.findViewById(android.R.id.tabhost);
        mTabHost.setup(getActivity(), getChildFragmentManager(), R.id.realtabcontent);

        mTabHost.addTab(mTabHost.newTabSpec("fragmentb").setIndicator("Fragment B"),
                StatisticsPage.class, null);
        mTabHost.addTab(mTabHost.newTabSpec("fragmentc").setIndicator("Fragment C"),
                BreweryStatistics.class, null);
        mTabHost.addTab(mTabHost.newTabSpec("fragmentd").setIndicator("Fragment D"),
                StyleStatistics.class, null);


        return rootView;
    }




}

这是选项卡中我的一个 fragment 的代码:

public class StyleStatistics extends Fragment implements GetStyleStatisticsJSON.OnArticleSelectedListener {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.style_statistics_layout, container, false);

        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
        String userName = prefs.getString("userName", null);
        String userID = prefs.getString("userID", null);


        String url = "myURL";

        //async task to get beer taste tag percents
        GetStyleStatisticsJSON task = new GetStyleStatisticsJSON(getActivity());
        task.setOnArticleSelectedListener(this);
        task.execute(url);

        // Inflate the layout for this fragment
        return v;

    }


    @Override
    public void onArticleSelected(String bID){

        //code to execute on click
        Fragment Fragment_one;
        FragmentManager man= getFragmentManager();
        FragmentTransaction tran = man.beginTransaction();
        Fragment_one = new StylePage2();
        final Bundle bundle = new Bundle();
        bundle.putString("beerIDSent", bID);
        Fragment_one.setArguments(bundle);
        tran.replace(R.id.main, Fragment_one);//tran.
        tran.addToBackStack(null);
        tran.commit();

    }


}

这是 xml fragment 的代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/styleStatsTitle"
        android:gravity="center"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:text="Your Top Styles"
        android:textSize="20sp"
        android:textStyle = "bold"
        android:padding="5dip"
        >
    </TextView>

    <ListView
        android:id="@+id/yourStyleStatistics"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:dividerHeight="0px"
        android:divider="@null"

        >
    </ListView>

</LinearLayout>

fragment_tab.xml:

<android.support.v4.app.FragmentTabHost
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        <FrameLayout
            android:id="@+id/realtabcontent"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1"/>





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

最佳答案

您可能需要在 FrameLayout 上方的 fragment_tabs.xml 中使用 TabWidget,例如:

<android.support.v4.app.FragmentTabHost
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        <TabWidget
            android:id="@android:id/tabs"
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0"/>

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

        <FrameLayout
            android:id="@+id/realtabcontent"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"/>

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

关于android - 选项卡位于 xml 中文本的顶部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22124124/

相关文章:

android - 当我在改造中使用 JsonObject 时,响应为 Null

android - ViewPager 不显示工具栏和选项卡

android - 如何为 Android fragment 中的多个按钮定义 setOnTouchListener

java - 将 stax XML 写入字符串

C XML 解析操作

android - 工具栏的向上插入符号打开导航菜单

android - 如何在 Fragment View 中访问 findViewById - Kotlin

android - 如何使用 Android Management API + Organisations 分发私有(private) Android 应用程序测试轨道?

Android GridView 在某些设备(三星 Galaxy S4、S3)上崩溃或速度非常慢

xml - Perl XML::LibXML 安装失败