我有一个包含三个部分的 DrawerLayout:其中一个是像 android studio 模板一样实现的两 Pane 布局,所以两个 fragment ,一个挨着另一个,包含在一个 LinearLayout 中。这是布局:
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="false"
android:orientation="vertical">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="false"
android:id="@+id/twoPaneLayut"
android:divider="?android:attr/dividerHorizontal"
android:showDividers="middle">
<FrameLayout
android:id="@+id/alarm_list_container"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
<FrameLayout
android:id="@+id/alarm_detail_container"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2" />
</LinearLayout>
</FrameLayout>
<ListView
android:id="@+id/left_drawer"
android:layout_width="@dimen/drawer_width"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
android:background="#111" />
现在,其他部分由单个 Fragment 组成,因此我试图用 FragmentTransaction 替换外部 FrameLayout 的主要内容。
问题是内层的LinearLayout明显没有去掉
那么最好的策略是什么?以编程方式删除 LinearLayout?将其可见性设置为 GONE?
最佳答案
FrameLayout
不能与 weightSum
配合使用,它的设计旨在填充您的布局。尝试改用 LinearLayout
或 RelativeLayout
。
其次,在交换所有内容时,您有几个选择。
选项 1
使用一个 FrameLayout
,为您的 2 面板布局创建一个包含 2 个 fragment 的 Fragment
。是的,您可以在 Fragment
中包含 Fragment
。
在这种情况下,您的 Activity
的布局看起来更像是 Google 推荐的实现方式,并且您的 fragment 布局将包含 2 个面板布局。
选项 2
您可以为 fragment 换出其中一个 LinearLayout
并隐藏其他 LinearLayout
。没有理由只能将 FrameLayout
换成 fragment ,您也应该能够将 LinearLayout
换成 Fragment
。
选项 3
DrawerLayout
实现只是一个建议。您可以将抽屉的布局移动到 fragment 中,并为不同的布局使用不同的 Activity
。需要明确的是,您的抽屉不是 ListView
,而是一个 fragment 。在这种情况下,将所有用于添加切换控件的代码放在一个帮助程序类中,这样您就可以轻松地重用它,您的抽屉 View 将位于 fragment 中,因此更容易包含在多个 Activity 中。
我提供了一个如何执行此操作的示例,因为您需要将抽屉导航 fragment 包装在框架布局中,并为框架布局提供 layout_gravity
属性,否则 fragment 将填满整个屏幕而不是关闭。
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<!-- The main content view -->
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- The navigation drawer -->
<FrameLayout
android:id="@+id/nav_drawer_container"
android:layout_gravity="start"
android:layout_width="240dp"
android:layout_height="match_parent" >
<fragment
android:id="@+id/nav_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
class="com.alimuzaffar.myapp.fragment.NavigationMenuFragment" />
</FrameLayout>
</android.support.v4.widget.DrawerLayout>
切换助手的代码可以类似于下面的 setupNavigationDrawer()
方法。只需从您想要抽屉导航的任何 Activity 的 onCreate()
调用此代码。
public static void setupNavigationDrawer() {
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
if (mDrawerLayout != null) {
// ActionBarDrawerToggle ties together the the proper interactions
// between the sliding drawer and the action bar app icon
mDrawerToggle = new ActionBarDrawerToggle(this, /* host Activity */
mDrawerLayout, /* DrawerLayout object */
R.drawable.ic_drawer, /* nav drawer image to replace 'Up' caret */
R.string.drawer_open, /* "open drawer" description for accessibility */
R.string.drawer_close /* "close drawer" description for accessibility */
) {
@Override
public void onDrawerClosed(View view) {
getSupportActionBar().setTitle(mTitle);
supportInvalidateOptionsMenu(); // creates call
// to
// onPrepareOptionsMenu()
}
@Override
public void onDrawerOpened(View drawerView) {
getSupportActionBar().setTitle("");
supportInvalidateOptionsMenu(); // creates call
// to
// onPrepareOptionsMenu()
}
};
// set a custom shadow that overlays the main content when the drawer opens
mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
mDrawerLayout.setDrawerListener(mDrawerToggle);
}
}
关于android - 两个 Pane 布局 + DrawerLayout : replace main content,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22731680/