Android 如何在线性布局中对齐图像垂直中心

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2113411/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:35:07  来源:igfitidea点击:

How can I align image vertical center in a Linear Layout

android

提问by hap497

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?

我有以下布局,左侧有 1 个图标,右侧有 2 个文本视图(堆叠在一起)。我想让 text1 在面板中垂直居中,当我使 text2 Visible.GONE 时。你能告诉我我该怎么做吗?

<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>

回答by Romain Guy

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

在第一个示例中,您所要做的就是android:gravity将 ImageView替换为android:layout_gravity。就这样 :)

回答by Alex Volovoy

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

我会将其更改为一种内部只有三个视图的相对布局。您也可以使用 android:layout_centerHorizontal, android:layout_centerVertical来对齐内容。