Joe*_*her 11 android navigation-drawer android-toolbar
我目前有一个带有标题的工具栏的导航抽屉,我希望将此标题置于工具栏中,但工具栏似乎没有考虑到抽屉图标,如下图所示.
而当我使用相同的工具栏布局用于不在导航抽屉内的其他活动时,标题完全居中,如下图所示:
那么如何让它考虑到这个图标呢?
这是我的布局:
<android.support.design.widget.AppBarLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:theme="@style/Theme.App.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/black"
app:popupTheme="@style/Theme.App.PopupOverlay">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/toolbar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textColor="@color/white"
android:textStyle="bold"
android:text="Title"
android:textSize="16sp" />
</RelativeLayout>
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
Run Code Online (Sandbox Code Playgroud)
Chi*_*ang 29
您需要了解Toolbar小部件扩展了ViewGroup类,并且它拥有自己的LayoutParams.
因此,您不需要在工具栏中使用RelativeLayout,并且需要使用TextView添加单行.
android:layout_gravity="center_horizontal"
Run Code Online (Sandbox Code Playgroud)
最终的xml应该是这样的,
<android.support.design.widget.AppBarLayout xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/Theme.App.AppBarOverlay" >
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/black"
app:popupTheme="@style/Theme.App.PopupOverlay" >
<TextView
android:id="@+id/toolbar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Title"
android:textColor="@color/white"
android:textSize="16sp"
android:textStyle="bold" />
</android.support.v7.widget.Toolbar>
Run Code Online (Sandbox Code Playgroud)
删除RelativeLayout,然后改变你TextView喜欢这样:
<android.support.design.widget.AppBarLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:theme="@style/Theme.App.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/black"
app:popupTheme="@style/Theme.App.PopupOverlay">
<TextView
android:id="@+id/toolbar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Title"
android:textColor="@color/white"
android:textSize="16sp"
android:textStyle="bold"/>
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
Run Code Online (Sandbox Code Playgroud)
在该电话之后setDisplayShowTitleEnabled,要删除原始标题,请在onCreate您的活动方法中:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
...
}
}
Run Code Online (Sandbox Code Playgroud)
这是获得的结果:
| 归档时间: |
|
| 查看次数: |
10938 次 |
| 最近记录: |