2015-10-10 123 views
4

您好,我正在学习使用本教程来创建材质设计。 https://www.youtube.com/watch?v=pMO8EVkhJO8&list=PLonJJ3BVjZW6CtAMbJz1XD8ELUs1KXaTD&index=3Android材质设计错误

在我的设计中,appbar在所有面上都有填充。有人可以帮我删除这个填充,使应用看起来像一个真正的应用栏。

这里是我的屏幕 enter image description here

app_bar

<?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="#DDDD"> 

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

主要活动

<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="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> 

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

    <TextView 
     android:layout_below="@+id/app_bar" 
     android:text="@string/hello_world" android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 

</RelativeLayout> 

maniAct ivity.java

private Toolbar toolbar; 

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

    toolbar = (Toolbar) findViewById(R.id.app_bar); 
    setSupportActionBar(toolbar); 
} 

回答

2

在您的相对布局中,您已将Paddings。你需要删除它们。

<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=".MainActivity"> 


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

    <TextView 
     android:layout_below="@+id/app_bar" 
     android:text="@string/hello_world" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 

</RelativeLayout> 

选中此项。现在工具栏上将没有填充。如果您想给其他孩子填充,则使用LinearLayoutorientationvertical制作一个父级布局,并将toolbarrelativelayout放入其中。从你的RelativeLayout布局

android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:paddingBottom="@dimen/activity_vertical_margin" 

或最佳方式下面的代码

2

删除使用下面的代码

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
     android:id="@+id/coordinator" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <android.support.design.widget.AppBarLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:theme="@style/ThemeOverlay.AppCompat.Dark"> 

      <include layout="@layout/toolbar"></include> 

      <android.support.design.widget.TabLayout 
       android:id="@+id/tablayout" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:background="?attr/colorPrimary" 
       app:tabGravity="fill"/> 

     </android.support.design.widget.AppBarLayout> 

     <android.support.v4.view.ViewPager 
      android:id="@+id/pager" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      app:layout_behavior="@string/appbar_scrolling_view_behavior"/> 
</android.support.design.widget.CoordinatorLayout> 
+0

很好的解决方案..!对我的作品。 – Vasu