如何去除Android Toolbar的左边距?

13 浏览
0 Comments

如何去除Android Toolbar的左边距?

我正在尝试在我的项目中使用工具栏。

以下是我正在使用的代码:

android:id="@+id/toolbar"

android:layout_width="match_parent"

android:layout_height="?attr/actionBarSize"

android:layout_alignParentTop="true"

android:background="?attr/colorPrimary"

android:contentInsetLeft="0dp"

android:elevation="@dimen/margin_padding_8dp"

android:contentInsetStart="0dp">

android:id="@+id/rlToolbar"

android:layout_width="match_parent"

android:layout_height="match_parent">

android:id="@+id/tvTitle"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_centerInParent="true"

android:paddingRight="@dimen/margin_padding_16dp"

android:text="AppBar"

android:textAppearance="@style/TextAppearance.AppCompat"

android:textColor="@color/white"

android:textSize="@dimen/text_size_20sp" />

toolbar

我想去掉左边的边距,我设置了android:contentInsetLeft="0dp"android:contentInsetStart="0dp",但是它不起作用..请帮帮我!

0
0 Comments

问题原因:Android的工具栏(Toolbar)在左侧有一定的空白间距。

解决方法:使用app:contentInsetStart="0dp"来去除左侧空白间距。

0
0 Comments

在Android的工具栏(Toolbar)上去除左边距的方法是在XML布局文件中添加app:contentInsetStart="0dp"这一行代码。这是因为在API 21之前(即Android Lollipop之前),需要添加这一行代码。

具体代码如下:

<android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="64dp"
            android:background="/colorPrimary"
            android:minHeight="?attr/actionBarSize"
            android:contentInsetStart="0dp"
            app:contentInsetStart="0dp"
            >
 </android.support.v7.widget.Toolbar>

0
0 Comments

问题:如何移除Android Toolbar的左边距?

原因:Toolbar默认会在左边添加一定的边距,导致左边出现空白。

解决方法:将原来的xml代码替换为以下代码:


    
        
    

如果有navigationIcon,添加以下代码:app:contentInsetStartWithNavigation="0dp"

0