Java 如何在片段内部使用布局?

Java 如何在片段内部使用布局?,java,android,Java,Android,我想在片段内部使用TabLayout。 这是我的fragment_main.xml: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" xmlns:app="

我想在片段内部使用TabLayout。 这是我的fragment_main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="com.ek.karhabtyapplication.ParkingFragment"
android:orientation="horizontal">

<android.support.design.widget.TabLayout
    android:id="@+id/simpleTabLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:tabBackground="@android:color/darker_gray" />

<FrameLayout
    android:id="@+id/simpleFrameLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
问题是,当我在第一个选项卡中单击时,消息正在闪烁,但片段没有改变。
任何人都有主意。

试试这段代码,在
LinearLayout中将
android:orientation=“horizontal”
设置为
android:orientation=“vertical”

 tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {

            switch (tab.getPosition()) {
                case 0:
            Fragment  fragmentOne = new ParkingMapFragment();
            Toast.makeText(getActivity(), "fragm 1 ", Toast.LENGTH_SHORT).show();

            FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
            transaction.add(R.id.simpleFrameLayout, fragment);
            transaction.commit();
                    break;
                case 1:
            Fragment  fragmentTwo = new ParkingHistoricFragment();
            Toast.makeText(getActivity(), "fragm 2 ", Toast.LENGTH_SHORT).show();
            FragmentTransaction transactionOne = getChildFragmentManager().beginTransaction();
            transactionOne .add(R.id.simpleFrameLayout, fragment);
            transactionOne .commit();
                    break;
            }


        }
});

片段会更改,但您无法看到它,因为simpleFrameLayout当前不在视图中。您的TableLayout正在填充整个屏幕宽度,并且您的FrameLayout位于TableLayout的右侧

您可以在选项卡布局下方添加一个视图寻呼机,然后您可以轻松地交换和使用它。创建一个视图寻呼机适配器并在该适配器中添加片段。

非常感谢。我将LinearLayout的方向更改为垂直,它可以工作;)感谢您的帮助:)private void setTabsViewPagerView(){setupViewPager(viewPager);TableLayout.setupWithViewPager(viewPager);SetupTabions();}private void setupViewPager(viewPager viewPager){ViewPagerAdapter=new ViewPagerAdapter(getChildFragmentManager());adapter.addFragment(新建ABCFrament(),getResources().getString(“abc”);adapter.addFragment(新建DefFragment(),getResources().getString(“测试”);viewPager.setAdapter(适配器);}
 tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {

            switch (tab.getPosition()) {
                case 0:
            Fragment  fragmentOne = new ParkingMapFragment();
            Toast.makeText(getActivity(), "fragm 1 ", Toast.LENGTH_SHORT).show();

            FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
            transaction.add(R.id.simpleFrameLayout, fragment);
            transaction.commit();
                    break;
                case 1:
            Fragment  fragmentTwo = new ParkingHistoricFragment();
            Toast.makeText(getActivity(), "fragm 2 ", Toast.LENGTH_SHORT).show();
            FragmentTransaction transactionOne = getChildFragmentManager().beginTransaction();
            transactionOne .add(R.id.simpleFrameLayout, fragment);
            transactionOne .commit();
                    break;
            }


        }
});