java如何在片段内部使用TablLayout?
我想在片段内部使用TabLayout。 这是我的主要部分。xml:
<LinearLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:tools="http://schemas.安卓.com/tools"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
xmlns:app="http://schemas.安卓.com/apk/res-auto"
tools:context="com.ek.karhabtyapplication.ParkingFragment"
安卓:orientation="horizontal">
<安卓.support.design.widget.TabLayout
安卓:id="@+id/simpleTabLayout"
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content"
app:tabBackground="@安卓:color/darker_gray" />
<FrameLayout
安卓:id="@+id/simpleFrameLayout"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent" />
这是我的主要片段。爪哇:
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
Fragment fragment = null;
switch (tab.getPosition()) {
case 0:
fragment = new ParkingMapFragment();
Toast.makeText(getActivity(), "fragm 1 ", Toast.LENGTH_SHORT).show();
break;
case 1:
fragment = new ParkingHistoricFragment();
Toast.makeText(getActivity(), "fragm 2 ", Toast.LENGTH_SHORT).show();
break;
}
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.add(R.id.simpleFrameLayout, fragment);
transaction.commit();
}
});
问题是,当我在第一个选项卡中单击时,消息正在闪烁,但片段没有改变。 谁都有主意
# 1 楼答案
片段会更改,但您无法看到它,因为simpleFrameLayout当前不在视图中。您的TableLayout正在填充整个屏幕宽度,并且您的FrameLayout位于TableLayout的右侧
# 2 楼答案
您可以在选项卡布局下方添加一个视图寻呼机,然后可以轻松地交换和使用它。创建一个视图寻呼机适配器并在该适配器中添加片段
# 3 楼答案
请尝试此代码,并在
LinearLayout
中将android:orientation="horizontal"
设置为android:orientation="vertical"