gpt4 book ai didi

android - 三 View 水平线性布局,中间 View 水平居中

转载 作者:行者123 更新时间:2023-11-29 00:56:51 48 4
gpt4 key购买 nike

我在水平 LinearLayout 中有三个项目:一个 TextView、一个 ImageButton 和另一个 TextView。这三个都是垂直居中的。

enter image description here

无论两个 TextViews 的宽度是多少,我怎样才能使中间的 ImageButton 完全水平居中?

也就是说:我想让 ImageButton 恰好在中心,左边的 TextView 应该看起来是右对齐的,右边的 TextView应该看起来是左对齐的。所有三个 View 都应垂直居中。

谢谢。

斯特凡

最佳答案

在线性布局中使用:

android:weightSum=“3”

并在您的 3 布局中的每个布局中使用以下属性

android:layout_weight=“1”
android:layout_width=“0dp”

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="3"
tools:context=".MainActivity">

<TextView
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content" />

<ImageButton
android:layout_weight="1"
android:layout_width="0"
android:layout_height="wrap_content" />

<TextView
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content" />



</LinearLayout>

请查看Android官方文档以查看LinearLayout的更多属性: https://developer.android.com/reference/android/widget/LinearLayout

关于android - 三 View 水平线性布局,中间 View 水平居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54045152/

48 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com