如何将三个视图与中间视图水平对齐?

时间:2018-08-17 11:43:51

标签: android android-layout android-linearlayout android-view

我想垂直对齐3个布局,以便三个文本视图中的每一个都恰好位于另一个之下。它有中间意见。 因此,我不能使用相对布局使用property layout_below。虽然我设法使上述两个布局与线性布局和权重对齐,但是第三个布局由一个图像视图组成,并且图像视图位于中心。    这是我的代码

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="start"
        android:layout_marginStart="5dp"
        android:orientation="horizontal">

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.3"
            android:text="@string/status_heading"
            android:textSize="11sp" />

        <ImageView
            android:layout_width="0dp"
            android:layout_height="15dp"


            android:layout_weight="0.3"
            android:src="@drawable/my_drawable" />

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.3"
            android:text="test"
            android:textSize="15sp" />
    </LinearLayout>

当有3个文本视图时,它可以正常工作,但是此图像视图以某种方式到达其自身的中心并且无法正确对齐。  谢谢:)

3 个答案:

答案 0 :(得分:0)

只需将Image与其他Layout包装在一起,使其不会居中,而是出现在开头:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginStart="5dp"
    android:gravity="start"
    android:orientation="horizontal">

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.3"
        android:text="@string/status_heading"
        android:textSize="11sp" />

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="15dp"
        android:layout_weight="0.3">

        <ImageView
            android:layout_width="15dp"
            android:layout_height="15dp"
            android:src="@drawable/my_drawable" />
    </LinearLayout>

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.3"
        android:text="test"
        android:textSize="15sp" />
</LinearLayout>

答案 1 :(得分:0)

尝试一下

autoplot

您可以根据自己的设计更改重量和weightSum

答案 2 :(得分:0)

文本视图的高度是自动换行的内容,因此大小取决于文本大小,具体取决于您的情况。现在,根据您的情况定义了图像的高度,因此显然不会对齐。

您可以做的是将父视图的高度和布局重力作为中心。

类似的东西-

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="20dp"
        android:gravity="start"
        android:layout_marginStart="5dp"
        android:orientation="horizontal">

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.3"
            android:layout_gravity="center"
            android:text="@string/app_name"
            android:textSize="11sp" />

        <ImageView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.3"
            android:src="@drawable/d"
            android:layout_gravity="center"
            android:scaleType="centerInside"/>

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.3"
            android:layout_gravity="center"
            android:text="test"
            android:textSize="15sp" />
    </LinearLayout>

您可以为文本视图的中心添加重力,以获得适当的水平空间。

相关问题