显示的AppCompat工具栏无效

问题描述:

我想在我的应用程序中添加AppCompat工具栏,但我无法以正确的方式显示它。我遵循本教程中的步骤:http://www.android4devs.com/2014/12/how-to-make-material-design-app.html,但工具栏与ListView重叠并且它们的一部分是分开的。我不知道如何描述它,所以这里是我的应用程序的屏幕: App screen显示的AppCompat工具栏无效

下面我附上代码的工具栏和我的看法。你能帮忙修复工具栏吗?

工具栏:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:background="@color/toolbar_primary" 
> 

</android.support.v7.widget.Toolbar> 

活动视图:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 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" 
android:paddingLeft="10dp" 
android:paddingRight="10dp" 
android:paddingTop="10dp" 
android:paddingBottom="10dp" 
tools:context="bak.grzegorz.pl.smm.Activities.MessageList"> 

<include 
    android:id="@+id/toolbar" 
    layout="@layout/toolbar" /> 

<ListView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@android:id/list" 
    android:layout_centerVertical="true" 
    android:layout_centerHorizontal="true" 
    /> 

</RelativeLayout> 

删除默认的老动作条

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 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" 
    tools:context="bak.grzegorz.pl.smm.Activities.MessageList"> 

    <include 
     android:id="@+id/toolbar" 
     layout="@layout/toolbar" /> 

    <ListView 
     android:layout_below="@+id/toolbar" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@android:id/list" 
     android:layout_centerVertical="true" 
     android:layout_centerHorizontal="true" 
     /> 

</RelativeLayout> 

在您的MainActivity设置工具栏中作为默认操作栏

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    Toolbar toolbar = (Toolbar) findViewById(R.id.tool_bar); 
    setSupportActionBar(toolbar); 

} 
+0

谢谢!我已经在Mudit Dangi的帖子后完成了,但是你的内容更加详细,可以帮助其他任何人。 – Blady214

它重复,因为你使用的是RelativeLayout的,无论是使用你的列表视图 “layout_below” 标签或使用的LinearLayout。您还在主RelativeLayout中添加了填充,以便修改您的工具栏的位置。通过使用您的RelativeLayout NoActionBar主题

style.xml

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> 
</style> 

AndroidMainifest.xml

<application 
    android:theme="@style/AppTheme" 
    ... > 

移除填充,并添加layout_below标志上的ListView

+0

Thanks!我没有想到这个解决方案如此简单。 – Blady214