使用layout_weight属性实现视图的居中显示

本文介绍如何使用LinearLayout的android:weightSum属性及子视图的android:layout_weight属性,实现按钮在不同Android设备上居中显示并占据屏幕宽度一半的效果。提供了一个具体的XML布局示例。

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

将按钮居中显示,并且占据其父视图宽度的一半,该如何做呢?

效果图如下:

竖屏

横屏

不同Android设备的尺寸往往是不同的。作为开发者,我们需要创建适用于不同尺寸屏幕的XML文件。硬编码是不可取的。我们可以结合LinearLayout的android:weightSum属性和LinearLayout的子视图的android:layout_weight属性来解决这个问题。

android:weightSum的开发文档里有一段描述如下:
“定义weight总和的最大值。如果未指定该值,以所有子视图的layout_weight属性的累加值作为总和的最大值。一个典型的案例是:通过指定指定子视图的layout_weight属性为0.5,并设置LinearLayout的android:weightSum属性为1.0.实现子视图占据可用宽度的50%。

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:background="#ffffff"
    android:gravity="center"
    android:orientation="horizontal"
    android:weightSum="1">

    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.5"
        android:text="Click me"/>

</LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <layout 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"> <data> <variable name="model" type="com.fshr.app.bean.sms.MyJobApplyBean" /> <import type="com.fshr.baseproject.utils.ConfigData" /> </data> <androidx.cardview.widget.CardView android:id="@+id/parent" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="8dp" app:cardCornerRadius="12dp" app:cardElevation="2dp"> <!-- Reduced elevation for a flatter look --> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:padding="16dp"> <!-- Increased padding --> <!-- Application ID --> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_vertical" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="申请表ID:" android:textColor="@color/grey_600" <!-- Lighter gray --> android:textSize="14sp" /> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginStart="8dp" android:layout_weight="1" android:text="@{model.id}" android:textColor="@color/black" android:textSize="16sp" android:textStyle="bold" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="8dp" android:background="@drawable/rounded_corner_reason_bg" android:gravity="center" android:paddingHorizontal="8dp" android:paddingVertical="4dp" android:text="@{ConfigData.INSTANCE.getParamLabel(ConfigData.INSTANCE.busi_work_appliacnt_status,model.applicationStatus,`待提交`)}" app:txColor="@{ConfigData.INSTANCE.getParamColor(ConfigData.INSTANCE.busi_work_appliacnt_status,model.applicationStatus)}" tools:text="Type" /> </LinearLayout> <!-- Applicant Information --> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="12dp" <!-- Increased spacing --> android:gravity="center_vertical" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="关联作业项目:" android:textColor="@color/grey_600" android:textSize="14sp" /> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginStart="8dp" android:layout_weight="1" android:text="@{model.project.projectName}" android:textColor="@color/black" android:textSize="16sp" /> </LinearLayout> <!-- Receiver Information --> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="12dp" android:gravity="center_vertical" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="申请编号:" android:textColor="@color/grey_600" android:textSize="14sp" /> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginStart="8dp" android:layout_weight="1" android:text="@{model.project.operationNo}" android:textColor="@color/black" android:textSize="16sp" /> </LinearLayout> <!-- Expected Access Time --> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="12dp" android:gravity="center_vertical" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="是否提级审批:" android:textColor="@color/grey_600" android:textSize="14sp" /> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginStart="8dp" android:layout_weight="1" android:text="@{ConfigData.INSTANCE.getParamLabel(ConfigData.INSTANCE.busi_common_yes_no,model.isHighLevel)}" android:textColor="@color/black" android:textSize="16sp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="12dp" android:gravity="center_vertical" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="是否有危险源辨识和风险评价表:" android:textColor="@color/grey_600" android:textSize="14sp" /> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginStart="8dp" android:layout_weight="1" android:text="@{ConfigData.INSTANCE.getParamLabel(ConfigData.INSTANCE.busi_common_yes_no,model.isHazard)}" android:textColor="@color/black" android:textSize="16sp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="12dp" android:gravity="center_vertical" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="是否有作业方案及评审情况:" android:textColor="@color/grey_600" android:textSize="14sp" /> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginStart="8dp" android:layout_weight="1" android:text="@{ConfigData.INSTANCE.getParamLabel(ConfigData.INSTANCE.busi_common_yes_no,model.isWorkPlan)}" android:textColor="@color/black" android:textSize="16sp" /> </LinearLayout> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="12dp" android:text="其他安全防护措施:" android:textColor="@color/grey_600" android:textSize="14sp" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="10dp" android:layout_marginTop="8dp" android:background="@drawable/rounded_corner_reason_bg" android:text="@{model.otherSafe }" android:textColor="@color/black" android:textSize="16sp" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="12dp" android:gravity="center_vertical" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/text_risk" android:layout_marginStart="8dp" android:text="管理危险辨识与风险评价" android:textColor="@color/blue" android:textSize="16sp" /> <TextView android:id="@+id/text_scheme" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginStart="8dp" android:layout_weight="1" android:gravity="right" android:visibility="@{model.isWorkPlan().equals(`1`)}" android:text="管理作业方案" android:textColor="@color/blue" android:textSize="16sp" /> </LinearLayout> <LinearLayout android:id="@+id/lin_btn" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="16dp" android:orientation="horizontal" android:visibility="@{!ConfigData.INSTANCE.jobJsUpdate(model.applicationStatus)}"> <Button android:id="@+id/btn_appover" android:layout_width="0dp" android:layout_height="40dp" android:layout_weight="1" android:background="@drawable/button_approve_background" android:stateListAnimator="@null" android:text="修改" android:textColor="@color/white" android:textSize="16sp" /> <Button android:id="@+id/btn_reject" android:layout_width="0dp" android:layout_height="40dp" android:layout_marginStart="16dp" android:layout_weight="1" android:background="@drawable/button_reject_background" android:stateListAnimator="@null" android:text="删除" android:textColor="@color/white" android:textSize="16sp" /> </LinearLayout> </LinearLayout> </androidx.cardview.widget.CardView> </layout> 从美观上帮我重新设计下排版,要求输入完整内容
最新发布
07-11
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值