使用Toolbar自定义布局时,设置match_parent 左右各有一段间距

本文详细介绍了如何解决Android中Toolbar组件的内边距问题,提供了通过XML属性直接设置及自定义style全局配置两种方法,确保Toolbar在应用中正确显示。

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

解决方法:

给Toolbar加上app:contentInsetStart”="0dp"(左侧)或者“app:contentInsetEnd”="0dp"(右侧)

<android.support.v7.widget.Toolbar
        android:layout_width="match_parent"
        android:layout_height="?android:actionBarSize"
        app:contentInsetStart="0dp"
        app:contentInsetEnd="0dp"
        app:contentInsetLeft="0dp"
        app:contentInsetRight="0dp">
 
        <!-- 自定义的布局-->
        <View
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@android:color/holo_green_dark"/>
    </android.support.v7.widget.Toolbar>

注意:

我们这里用的是v7包下的Toolbar,所以要使用自定义的属性。如果你使用的是系统的Toolbar,则使用android:开头的属性才会生效。

 

或者通过自定义style去全局设置

此文通过学习博客: https://blog.youkuaiyun.com/afei__/article/details/51476096

做的笔记

<?xml version="1.0" encoding="utf-8"?> <FrameLayout 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/dialog_layout" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginBottom="@dimen/dimen_84dp" android:orientation="vertical"> <com.google.android.material.appbar.COUIDividerAppBarLayout android:id="@+id/appbar" style="@style/CommonAppBarStyle" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/color_transparent" android:clickable="true" android:focusable="true" android:paddingLeft="@dimen/dimen_0dp" android:paddingRight="@dimen/dimen_0dp" app:elevation="@dimen/toolbar_elevation"> <com.coui.appcompat.toolbar.COUIToolbar android:id="@+id/toolbar" style="@style/COUIToolBarInAppBarLayoutStyle" android:layout_width="match_parent" android:background="@null" app:supportTitleTextAppearance="@style/textAppearanceSecondTitle" app:titleCenter="false" /> <com.coui.appcompat.tablayout.COUITabLayout android:id="@+id/tab_layout" android:background="@color/color_transparent" style="@style/COUISmallTabLayoutStyle" /> </com.google.android.material.appbar.COUIDividerAppBarLayout> <RelativeLayout android:id="@+id/content_layout" android:layout_width="match_parent" android:layout_height="match_parent"> <com.filemanager.common.view.ViewPagerWrapperForPC android:id="@+id/view_pager_wrapper" android:layout_width="match_parent" android:layout_height="match_parent"> <com.filemanager.common.view.viewpager.RTLViewPager android:id="@+id/viewPager" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_below="@+id/appbar" android:clipChildren="false" android:clipToPadding="false" android:orientation="horizontal" /> </com.filemanager.common.view.ViewPagerWrapperForPC> </RelativeLayout> </LinearLayout> <!-- 其他布局保持不变... --> <FrameLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="bottom" android:background="?attr/couiColorBackgroundElevatedWithCard" android:minHeight="@dimen/operation_btn_background_height"> <View android:id="@+id/button_divider" android:layout_width="match_parent" android:layout_height="@dimen/divider_background_height" android:layout_gravity="top" android:alpha="0" android:background="?attr/couiColorDivider" android:forceDarkAllowed="false" /> <!-- 修改后的底部操作栏布局 --> <androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent" android:layout_height="match_parent"> <!-- 文本区域添加约束和最大宽度 --> <LinearLayout android:id="@+id/select_root_view" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginStart="@dimen/dimen_16dp" android:orientation="vertical" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toStartOf="@id/btn_add_file" app:layout_constraintHorizontal_bias="0" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintWidth_max="wrap" app:layout_constraintWidth_percent="0.7"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <!-- 添加最大行数和省略号处理 --> <TextView android:id="@+id/select_title_content" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:ellipsize="end" android:maxLines="2" android:text="@string/not_selected_file" android:textSize="16sp" android:lineHeight="22dp" android:textColor="?attr/couiColorLabelPrimary" android:textAppearance="@style/couiTextHeadlineXS" android:visibility="visible" tools:ignore="SpUsage" /> <ImageView android:id="@+id/select_arraw_up" android:layout_width="18dp" android:layout_height="18dp" android:src="@drawable/arrow_up" android:layout_marginLeft="@dimen/dimen_6dp" android:visibility="gone" android:layout_gravity="center"/> </LinearLayout> <!-- 添加最大行数和省略号处理 --> <TextView android:id="@+id/select_body_content" android:layout_width="match_parent" android:layout_height="wrap_content" android:lineHeight="20dp" android:textSize="14sp" android:textAppearance="?attr/couiTextBodyXS" android:textColor="?attr/couiColorLabelSecondary" android:visibility="gone" android:ellipsize="end" android:maxLines="1" android:text="@string/selected_size"/> </LinearLayout> <!-- 按钮添加约束 --> <com.coui.appcompat.button.COUIButton android:id="@+id/btn_add_file" style="@style/Widget.COUI.Button.Large" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginEnd="@dimen/dimen_16dp" android:minWidth="@dimen/dimen_96dp" android:text="@string/label_add_recent_file" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout> </FrameLayout> </FrameLayout>这一版在文字长的候会显示两行,能不能让文字居中显示在所在组件中
最新发布
07-29
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/gradient_background"> <!-- 标题栏 --> <RelativeLayout android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?android:attr/actionBarSize" android:background="#44bd32"> <Button android:id="@+id/fanHui" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginStart="8dp" android:layout_marginEnd="8dp" android:background="@android:color/transparent" android:foreground="?android:attr/selectableItemBackground" android:text="返回" android:textColor="@android:color/white" android:textSize="16sp" /> </RelativeLayout> <!-- 底部按钮 --> <LinearLayout android:id="@+id/footer_buttons" android:layout_width="match_parent" android:layout_height="1dp" android:layout_alignParentBottom="true" android:background="@android:color/white" android:orientation="horizontal"> <!-- <Button--> <!-- android:id="@+id/device_location"--> <!-- android:layout_width="match_parent"--> <!-- android:layout_height="match_parent"--> <!-- android:gravity="center"--> <!-- android:text="设备定位"--> <!-- android:textSize="19sp"--> <!-- android:background="#44bd32"--> <!-- android:textColor="@android:color/white" />--> </LinearLayout> <RelativeLayout android:layout_width="match_parent" android:layout_height="380dp" android:layout_above="@id/footer_buttons" android:layout_below="@id/toolbar" android:layout_marginBottom="298dp" android:background="@android:color/black" android:padding="0dp"> <!-- 视频播放器 --> <VideoView android:id="@+id/video_player" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_centerInParent="true" /> <!-- 播放控制条 --> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:background="#99000000" android:orientation="horizontal" android:padding="8dp"> <ImageButton android:id="@+id/btn_play_pause" android:layout_width="40dp" android:layout_height="40dp" android:background="?attr/selectableItemBackgroundBorderless" android:src="@drawable/ic_play_sel" /> <SeekBar android:id="@+id/video_seekbar" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:layout_gravity="center_vertical" /> <TextView android:id="@+id/tv_time" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:text="00:00/00:00" android:textColor="@android:color/white" android:textSize="14sp" /> </LinearLayout> </RelativeLayout> </RelativeLayout> 在视频播放器模块下面屏幕剩余位置添加一个放置缩略图的模块<ImageView android:layout_width="0dp" android:layout_height="wrap_content" app:layout_columnWeight="1" android:layout_margin="4dp" android:adjustViewBounds="true" android:contentDescription="萤石缩略图" android:src="@drawable/default_thumbnail" />
06-27
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值