我试图在另一个片段下方显示片段,但我遇到了视图问题,当我调用应该在第一个片段下方的第二个片段时,它是重叠的。
我的第一个片段容器RelativeLayout
Id mainLayout
,第二个片段的 Id carLayout
。
我假设XML
代码的问题。
我的 XML
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/topLayout">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="@+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:elevation="4dp"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
android:layout_weight="1" />
</LinearLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/mainLayout">
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/carLayout">
</RelativeLayout>
</LinearLayout>
我需要在片段 2 下方设置片段 1
但它是重叠的。
据
我了解,您需要在同一视图中设置两个片段
// add your first fragment
FragmentTransaction transaction = getFragmentManager().beginTransaction().add(frame_container1 , YOUR_FIRST_FRAGMENT).commit;
// add your second fragment
FragmentTransaction transaction = getFragmentManager().beginTransaction().add(frame_container2 , YOUR_SECOND_FRAGMENT).commit;
<linearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="2">
<FrameLayout
android:id="@+id/frame_container1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" />
<FrameLayout
android:id="@+id/frame_container2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"/>
</linearLayout>
或者干脆
<fragment
xmlns:android="http://schemas.android.com/apk/res/android"
android:name="YOUR FRAGMENT CLASS"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
</fragment>
carLayout 是一个相对布局,所以新的片段将覆盖最后一个片段,将其更改为方向垂直的 LinearLayout。
您需要
在carLayout
下方查看另一个视图。
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/anotherLayout"
android:layout_below="@id/carLayout">
</RelativeLayout>
"add"方法将片段添加到视图中,因此您可以修改方法以每次使用不同的容器:
public void replaceFragment(Fragment someFragment, int containerId) {
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.add(containerId , someFragment);
transaction.addToBackStack(null);
transaction.commit();
}
case R.id.home_menu:
fragment = new mainFrog();
replaceFragment(fragment,R.id.carLayout);
public void Haraj_cars(View view) {
fragment = new carFrog();
replaceFragment(fragment,R.id.anotherLayout);
}