如何将图像垂直中心对齐到线性布局中?

时间:2022-11-25 00:09:46

I have the following layout, which is 1 icon on the left and 2 text views (stack on top of each other) on the right. I would like to have the text1 vertically center in the panel and when I make the text2 Visible.GONE. Can you please tell me how can i do that?

我有下面的布局,左边是一个图标,右边是两个文本视图(叠加在一起)。我想让text1在面板中垂直居中,当我让text2 visible。消失。你能告诉我怎么做吗?

<RelativeLayout android:id="@+id/panel"
        android:layout_width="fill_parent"
        android:layout_height="?android:attr/listPreferredItemHeight">
    <ImageView
        android:id="@+id/icon1"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_gravity="center_vertical"/>
    <TextView android:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/icon1"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:layout_gravity="center_vertical"/>
    <TextView android:id="@+id/text2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/icon1"
        android:layout_below="@id/text1"
        android:textAppearance="?android:attr/textAppearanceSmall"/>
    </RelativeLayout>

2 个解决方案

#1


15  

All you have to do is, in your first example, to replace android:gravity on the ImageView by android:layout_gravity. That's all :)

在第一个示例中,您所要做的就是用android:layout_gravity取代ImageView上的android:gravity。这就是:)

#2


9  

I'd change it to one relative layout with only three views inside. And you can use android:layout_centerHorizontal, android:layout_centerVertical to align stuff as well.

我把它改成一个相对布局,里面只有三个视图。你也可以使用android:layout_centerHorizontal, android:layout_centerVertical来对齐内容。

#1


15  

All you have to do is, in your first example, to replace android:gravity on the ImageView by android:layout_gravity. That's all :)

在第一个示例中,您所要做的就是用android:layout_gravity取代ImageView上的android:gravity。这就是:)

#2


9  

I'd change it to one relative layout with only three views inside. And you can use android:layout_centerHorizontal, android:layout_centerVertical to align stuff as well.

我把它改成一个相对布局,里面只有三个视图。你也可以使用android:layout_centerHorizontal, android:layout_centerVertical来对齐内容。