Android FrameLayout子view居中(左居中,右居中)等

Android的布局FrameLayout默认是把布局内的子view堆砌在左上角,但是,可以通过设置子view的:

android:layout_gravity

此参数控制子view的布局位置,实现FrameLayout的各种子view布局位置,如左居中,右居中,居中等等,要点在android:layout_gravity的参数设置中组合使用“center”、“left”、“right”等,现在给出XML代码实例布局:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@android:color/black"
        android:text="左上角"
        android:textColor="@android:color/white" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="left|center"
        android:background="@android:color/black"
        android:text="左居中"
        android:textColor="@android:color/white" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:background="@android:color/black"
        android:text="右上角"
        android:textColor="@android:color/white" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right|center"
        android:background="@android:color/black"
        android:text="右居中"
        android:textColor="@android:color/white" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:background="@android:color/black"
        android:text="居中"
        android:textColor="@android:color/white" />

</FrameLayout>

运行结果如图所示:

### 实现 Android 控件居中显示 #### 使用 LinearLayout 和 `gravity` 属性 当使用 `LinearLayout` 作为父布局时,可以通过设置 `android:gravity="center"` 来使所有视图在其内部居中[^1]。 ```xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:gravity="center"> <TextView android:text="Centered Text" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout> ``` #### 利用 RelativeLayout 的属性 对于 `RelativeLayout`,可以利用 `android:layout_centerInParent="true"` 或者组合使用 `android:layout_centerHorizontal="true"` 和 `android:layout_centerVertical="true"` 来达到同样的目的。 ```xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Click Me!" android:layout_centerInParent="true"/> </RelativeLayout> ``` #### ConstraintLayout 中心约束 在现代开发实践中推荐使用的 `ConstraintLayout` 可以通过给目标 View 设置水平和垂直方向上的中心约束来完成居中操作。 ```xml <androidx.constraintlayout.widget.ConstraintLayout 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"> <ImageView android:id="@+id/imageView" android:layout_width="wrap_content" android:layout_height="wrap_content" app:srcCompat="@drawable/ic_launcher_background" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout> ``` #### FrameLayout 下的居中方式 如果是在 `FrameLayout` 内部,则应该考虑应用 `android:layout_gravity="center"` 给具体的元素使其位于中央位置[^3]。 ```xml <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:text="This is centered text." android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center"/> </FrameLayout> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zhangphil

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值