RelativeLayout 填充影响我的工具栏
RelaiveLayout's paddings are effecting my ToolBar
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
>
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar_top"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
android:background="@color/colorPrimary"
android:fitsSystemWindows="true"
>
<TextView
android:id="@+id/toolbar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/TextAppearance.AppCompat.Widget.ActionBar.Title"
android:layout_gravity="center" />
</android.support.v7.widget.Toolbar>
.
.
.
//etc
</RelativeLayout>
在我的 Activity,
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_top);
TextView textView = (TextView) toolbar.findViewById(R.id.toolbar_title);
textView.setText("My Title");
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
然后..,
工具栏显示如下..
我使用自定义工具栏的方式似乎很奇怪。我应该把我的工具栏放在哪里?我整晚都在尝试放置自定义工具栏。有什么建议吗?
试试这个
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:background="@drawable/background"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar_top"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
android:fitsSystemWindows="true"
android:minHeight="?attr/actionBarSize">
<TextView
android:id="@+id/toolbar_title"
style="@style/TextAppearance.AppCompat.Widget.ActionBar.Title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
</android.support.v7.widget.Toolbar>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">
</RelativeLayout>
</LinearLayout>
不要把Toolbar放在相对布局里面,而是把相对布局放在toolbar下面,如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/thisLayoutisTheRootOne"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar_top"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
android:background="@color/colorPrimary"
android:fitsSystemWindows="true">
<TextView
android:id="@+id/toolbar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/TextAppearance.AppCompat.Widget.ActionBar.Title"
android:layout_gravity="center" />
</android.support.v7.widget.Toolbar>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/toolbar_top"
android:background="@drawable/background"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">
.
.
.
//etc
</RelativeLayout>
</RelativeLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.nc2_44.ppaassignment13.activities.AddPostActivity">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbarAddPost"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/titleColor"
app:navigationIcon="@drawable/back_icon">
<TextView
android:layout_width="wrap_content"
android:layout_height="@dimen/d30dp"
android:layout_gravity="center|center_vertical"
android:fontFamily="@font/roboto_medium"
android:text="@string/add_post"
android:textAllCaps="false"
android:textColor="@android:color/white"
android:textSize="@dimen/s23sp" />
</android.support.v7.widget.Toolbar>
<RelativeLayout
android:id="@+id/relativeContents"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/toolbarAddPost">
<!-- Put your content here -->
</RelativeLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
>
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar_top"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
android:background="@color/colorPrimary"
android:fitsSystemWindows="true"
>
<TextView
android:id="@+id/toolbar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/TextAppearance.AppCompat.Widget.ActionBar.Title"
android:layout_gravity="center" />
</android.support.v7.widget.Toolbar>
.
.
.
//etc
</RelativeLayout>
在我的 Activity,
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_top);
TextView textView = (TextView) toolbar.findViewById(R.id.toolbar_title);
textView.setText("My Title");
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
然后..,
工具栏显示如下..
我使用自定义工具栏的方式似乎很奇怪。我应该把我的工具栏放在哪里?我整晚都在尝试放置自定义工具栏。有什么建议吗?
试试这个
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:background="@drawable/background"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar_top"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
android:fitsSystemWindows="true"
android:minHeight="?attr/actionBarSize">
<TextView
android:id="@+id/toolbar_title"
style="@style/TextAppearance.AppCompat.Widget.ActionBar.Title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
</android.support.v7.widget.Toolbar>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">
</RelativeLayout>
</LinearLayout>
不要把Toolbar放在相对布局里面,而是把相对布局放在toolbar下面,如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/thisLayoutisTheRootOne"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar_top"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
android:background="@color/colorPrimary"
android:fitsSystemWindows="true">
<TextView
android:id="@+id/toolbar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/TextAppearance.AppCompat.Widget.ActionBar.Title"
android:layout_gravity="center" />
</android.support.v7.widget.Toolbar>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/toolbar_top"
android:background="@drawable/background"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">
.
.
.
//etc
</RelativeLayout>
</RelativeLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.nc2_44.ppaassignment13.activities.AddPostActivity">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbarAddPost"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/titleColor"
app:navigationIcon="@drawable/back_icon">
<TextView
android:layout_width="wrap_content"
android:layout_height="@dimen/d30dp"
android:layout_gravity="center|center_vertical"
android:fontFamily="@font/roboto_medium"
android:text="@string/add_post"
android:textAllCaps="false"
android:textColor="@android:color/white"
android:textSize="@dimen/s23sp" />
</android.support.v7.widget.Toolbar>
<RelativeLayout
android:id="@+id/relativeContents"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/toolbarAddPost">
<!-- Put your content here -->
</RelativeLayout>