2015-10-04 89 views
0

我想在我的應用程序中添加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> 

回答

1

刪除默認的老動作條

<?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

1

它重複,因爲你使用的是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