Android appcompat v21材质设计问题:片段覆盖内容和工具栏
我随后使用了工具栏,没有片段,它工作得很好,但当我使用片段时,片段总是覆盖在内容上,显示片段时我看不到工具栏 屏幕: 当我从当前活动启动另一个活动时,新活动中工具栏的图标不会显示。屏幕: 我使用Android支持库21.0.3 以及MainActivity类中的代码:Android appcompat v21材质设计问题:片段覆盖内容和工具栏,android,android-fragments,android-appcompat,android-toolbar,Android,Android Fragments,Android Appcompat,Android Toolbar,我随后使用了工具栏,没有片段,它工作得很好,但当我使用片段时,片段总是覆盖在内容上,显示片段时我看不到工具栏 屏幕: 当我从当前活动启动另一个活动时,新活动中工具栏的图标不会显示。屏幕: 我使用Android支持库21.0.3 以及MainActivity类中的代码: protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
mLeftDrawerList = (ListView) findViewById(R.id.left_drawer);
mToolbar = (Toolbar) findViewById(R.id.toolbar);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
mNavigationDrawerAdapter=new ArrayAdapter<String>( MyActivity.this, android.R.layout.simple_list_item_1, data);
mLeftDrawerList.setAdapter(mNavigationDrawerAdapter);
if (mToolbar != null) {
mToolbar.setTitle("Home");
setSupportActionBar(mToolbar);
}
initDrawer();
final FragmentManager fragementMgr = getSupportFragmentManager();
FragmentTransaction transaction = fragementMgr.beginTransaction();
transaction.add(R.id.fragment_container, new MyFragment());
transaction.commit();
}
片段的布局xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello_fragment"
android:textStyle="bold"
android:gravity="center"
android:layout_gravity="top"
android:textSize="18sp"
android:id="@+id/text"/>
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_below="@+id/text"
android:text="@string/open"
android:id="@+id/button"/>
</RelativeLayout>
详细信息活动的布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent"
tools:context=".DetailActivity">
<include layout="@layout/toolbar" />
<TextView
android:layout_width="wrap_content"
android:textColor="#000"
android:text="Detail Activity Content"
android:layout_height="wrap_content" />
</LinearLayout>
有人能帮我吗?非常感谢
更新:我根据m iav的回复更新了主要活动的代码和布局
新屏幕现在为空白,有以下更改:
这应该是您的主要布局
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity"
android:layout_width="match_parent"
android:id="@+id/drawerLayout"
android:layout_height="match_parent">
<!-- activity view -->
<LinearLayout
android:layout_width="match_parent"
android:background="#fff"
android:layout_height="match_parent">
<include layout="@layout/toolbar" />
<FrameLayout android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<!-- navigation drawer -->
<RelativeLayout
android:layout_gravity="left|start"
android:layout_width="match_parent"
android:background="#fff"
android:layout_height="match_parent">
<ListView
android:id="@+id/left_drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="#eee"
android:background="#fff"
android:dividerHeight="1dp" />
</RelativeLayout>
</android.support.v4.widget.DrawerLayout>
关于第二个问题,导航抽屉不能在不同的活动之间共享。你必须使用一个新的片段,并保持你现在使用的活动。工具栏是布局中的一个视图,你可以像textview一样看到它,现在它是一个textview覆盖你的片段,你可以使用attrbibu在它下面所以最后在检查示例:MaterialEverywhere之后,我提供了更新布局的解决方案:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity"
android:layout_width="match_parent"
android:id="@+id/drawerLayout"
android:layout_height="match_parent">
<!-- activity view -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
android:id="@+id/toolbar"
layout="@layout/toolbar" />
<FrameLayout android:id="@+id/fragment_container"
android:layout_below="@id/toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
<!-- navigation drawer -->
<RelativeLayout
android:layout_gravity="left|start"
android:layout_width="match_parent"
android:background="#fff"
android:layout_height="match_parent">
<ListView
android:id="@+id/left_drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="#eee"
android:background="#fff"
android:dividerHeight="1dp" />
</RelativeLayout>
</android.support.v4.widget.DrawerLayout>
技巧是工具栏和内容框架的父布局。我使用RelativeLayout而不是LinearLayout。现在它的工作原理与预期一致
无论如何,我发现SDK中的这种更改过于复杂,更改太多,每一个都可能导致问题。开发人员使用干净的API,减少所有这些调整,这会更好。谢谢m iav!我尝试了你的解决方案,但是片段没有显示出来。文本和按钮不显示。看起来很奇怪。活动中的代码是:transaction.add(R.id.fragment_容器,new MyFragment());commit();确保您在上面的
FrameLayout
中有android:id=“@+id/fragment\u container”
。请更新原始问题中的代码以反映您的更改。我看不出您的代码有任何错误。我唯一的猜测是您使用的是android.app.FragmentManager
,而不是android.support.v4.app.FragmentManager
。尝试嵌套所有四行,如:getSupportFragmentManager().beginTransaction().add(…).commit()
HI m iav,实际上我使用了android.support.v4.app.FragmentManager,否则会出现兼容错误。您还可以看到上面MyActivity的代码,这是主要活动。可能是因为布局的原因吗?在主活动的布局中,LinearLayout中的FrameLayout具有布局高度匹配父项?谢谢!是的,解决方案与此非常接近。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent"
tools:context=".DetailActivity">
<include layout="@layout/toolbar" />
<TextView
android:layout_width="wrap_content"
android:textColor="#000"
android:text="Detail Activity Content"
android:layout_height="wrap_content" />
</LinearLayout>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity"
android:layout_width="match_parent"
android:id="@+id/drawerLayout"
android:layout_height="match_parent">
<!-- activity view -->
<LinearLayout
android:layout_width="match_parent"
android:background="#fff"
android:layout_height="match_parent">
<include layout="@layout/toolbar" />
<FrameLayout android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<!-- navigation drawer -->
<RelativeLayout
android:layout_gravity="left|start"
android:layout_width="match_parent"
android:background="#fff"
android:layout_height="match_parent">
<ListView
android:id="@+id/left_drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="#eee"
android:background="#fff"
android:dividerHeight="1dp" />
</RelativeLayout>
</android.support.v4.widget.DrawerLayout>
transaction.add(R.id.fragment_container, new MyFragment())
transaction.commit();
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity"
android:layout_width="match_parent"
android:id="@+id/drawerLayout"
android:layout_height="match_parent">
<!-- activity view -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
android:id="@+id/toolbar"
layout="@layout/toolbar" />
<FrameLayout android:id="@+id/fragment_container"
android:layout_below="@id/toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
<!-- navigation drawer -->
<RelativeLayout
android:layout_gravity="left|start"
android:layout_width="match_parent"
android:background="#fff"
android:layout_height="match_parent">
<ListView
android:id="@+id/left_drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="#eee"
android:background="#fff"
android:dividerHeight="1dp" />
</RelativeLayout>
</android.support.v4.widget.DrawerLayout>