Android Toolbar Padding

本文详细介绍了在 API version 21 中正确使用 Toolbar 的方法。针对布局中出现的额外边距问题,通过调整 padding 和 contentInset 属性来实现正常显示。文中还提供了具体的 XML 布局代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

版权声明:本文为博主原创文章,未经博主允许不得转载。(这是本人博客园博客也有同类文章


在API version 21 (the support library),如何正常使用Toolbar

这是常规式布局出来的Toolbar,我设置了bg,明显左边和上面都有边距:enter image description here

 

然后,我更改后的代码如下:

<android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:background="?attr/colorPrimary"
        android:padding="0dp"
        android:layout_margin="0dp">

        <RelativeLayout
            android:id="@+id/action_bar_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="0dp"
            android:padding="0dp"
            android:background="#000000">

            <Spinner
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>

        </RelativeLayout>
</Toolbar>

你会发现设置padding为0这个属性是没有用的,直接看下面效果图:

 

这显示我设置的spinner为72dp了。 desc

Edit 1

我需要正常的显示我的spinner,去掉padding,Here I going to find Google API:

 desc

Edit 2

下面我将contentInsetStart设置为0。您将需要使用的支持库应用程序名称空间:

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

     <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:minHeight="@dimen/action_bar_height"
        android:background="?attr/colorPrimary"
        android:contentInsetStart="0dp"
        android:contentInsetLeft="0dp"
        app:contentInsetLeft="0dp"
        app:contentInsetStart="0dp"
        app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light">

</android.support.v4.widget.DrawerLayout>

希望能帮到别人,这个困扰了我近一天的question!

版权声明:本文为博主原创文章,未经博主允许不得转载。

Android中,ActionBar(Action Bar)通常位于应用程序的顶部,它包含应用标题、菜单按钮以及操作选项等。如果你想要更改返回箭头图标的内边距(padding),这涉及到自定义布局和主题。 首先,你需要创建一个自定义的`Toolbar`视图或者使用默认的`AppCompatActivity`并设置自定义的`Theme`. 在XML布局文件中,你可以设置`<androidx.appcompat.widget.Toolbar>`的`contentInsetStart`属性来影响返回图标的位置: ```xml <com.google.android.material.appbar.MaterialToolbar android:id="@+id/my_toolbar" style="@style/MyCustomToolBarStyle" app:title="Title" app:contentInsetStart="custom_padding"> <ImageView android:id="@+id/back_button" android:src="@drawable/ic_back" android:padding="custom_padding"/> </com.google.android.material.appbar.MaterialToolbar> ``` 在这里,`custom_padding`是你想要设定的内边距值,可以使用像素dp或者sp单位。 然后,在你的样式文件`styles.xml`中定义主题,增加对`contentInsetStart`的支持: ```xml <style name="MyCustomToolBarStyle" parent="Widget.MaterialComponents.Toolbar.Primary"> <!-- 其他样式 --> <item name="contentInsetStart">?attr/actionBarSize + custom_padding</item> </style> ``` 这样,当你将这个主题应用于Activity时,返回箭头图标会按照设置的内边距进行调整。 注意:`?attr/actionBarSize`获取的是ActionBar的高度,加上自定义的`custom_padding`,确保数值合理,不会影响到其他UI元素。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值