具有三个视图的水平LinearLayout,中间视图应水平居中

时间:2019-01-04 19:47:59

标签: android android-linearlayout

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

enter image description here

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

也就是说,我想将ImageButton放在确切的中心,左边的TextView应该看起来右对齐,右边的TextView应该看起来左对齐。所有三个视图应垂直居中。

谢谢。

Stefan

5 个答案:

答案 0 :(得分:0)

您最好为他们设置体重。只是玩重物。

如果重量恒定,无论什么情况,它们总是会填充特定的空间。

1-设置所有视图的宽度以匹配父视图。

2-将所有重量设置为1。

然后确保您的图像位于这些文本视图的中心

 <EditText
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:gravity="top"
        android:hint="@string/message" />

答案 1 :(得分:0)

在LinearLayout中使用:

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

答案 2 :(得分:0)

孩子必须有:

1-layout_width =“ 0dp”

2-layout_weight =“ 1”

这是您需要的实现:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:id="@+id/textView1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="This is a first text" />

    <ImageButton
        android:id="@+id/imageButton"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        tools:srcCompat="@drawable/avatar" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="This is second text that has a long content than the first one" />

</LinearLayout>

答案 3 :(得分:0)

使用版式权重。检查以下示例:

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/layout_cancel_x_next"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

       <TextView
           android:id="@+id/text1"
           android:layout_width="@dimen/dimen_0dp"
           android:layout_weight="1"
           android:gravity="center_vertical"
           android:layout_height="match_parent"
           android:text="text1"
           />
        <ImageView
            android:id="@+id/b"
            android:layout_width="@dimen/dimen_0dp"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:src="@drawable/ic_delete_button"
            android:layout_weight="1"
            />
        <TextView
            android:id="@+id/text2"
            android:layout_width="@dimen/dimen_0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:text="text2"/>

    </LinearLayout>

答案 4 :(得分:0)

请尝试这个...

<?xml version="1.0" encoding="utf-8"?><?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="wrap_content"
    android:gravity="center_vertical">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="end"
        android:text="This is a first text" />

    <ImageButton
        android:id="@+id/imageButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@mipmap/ic_launcher" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="start"
        android:text="This is second text" />
</LinearLayout>