当在Android工具栏中添加自定义视图时,将会有一个左边缘



我对android工具栏有一些问题。通常,如果我在工具栏中设置自定义视图,该视图应该从左到右填充整个工具栏空间,并且没有边距。

但是我的左边有一个空格,这些是我的代码:

xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <android.support.v7.widget.Toolbar
        android:id="@+id/base_toolbar"
        android:layout_width="match_parent"
        android:layout_height="46dip"
        android:background="?attr/colorPrimary" />
    <FrameLayout
        android:id="@+id/base_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>
活动:

private void initToolbar() {
    toolbar = (Toolbar) findViewById(R.id.base_toolbar);
    setSupportActionBar(toolbar);
    ActionBar actionBar = getSupportActionBar();
    if( actionBar != null)
        actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
    contentLayout = (FrameLayout) findViewById(R.id.base_content);
}

是我的代码有什么问题,还是应该是这样的?

左边的空间是由工具栏的contentInsetStart(默认为16dp)引起的。您可以将其设置为0以删除它。不要忘记添加模式。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <android.support.v7.widget.Toolbar
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/base_toolbar"
        android:layout_width="match_parent"
        android:layout_height="46dip"
        android:background="?attr/colorPrimary"
        app:contentInsetLeft="0dp"
        app:contentInsetStart="0dp" />
    <FrameLayout
        android:id="@+id/base_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>

你可以这样做,

<android.support.v7.widget.Toolbar
        android:id="@+id/my_awesome_toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#1E90FF"
        android:minHeight="?attr/actionBarSize" >
        <TextView
            android:id="@+id/toolbar_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:ellipsize="end"
            android:singleLine="true"
            android:text="Toolbar Title"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textColor="#ffffff" />
    </android.support.v7.widget.Toolbar>

相关内容

最新更新