<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>
在我的活动中,
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);
似乎我以奇怪的方式使用我的自定义工具栏。我应该在哪里放置我的工具栏?我整晚都试图放置自定义工具栏。有什么建议吗?
答案 0 :(得分:2)
试试这个
<?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>
答案 1 :(得分:2)
不要将工具栏放在相对布局中,而是将相对布局设置在工具栏下方,如下所示:
<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>
答案 2 :(得分:1)
<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>