Java 如何在其父级中垂直居中 ImageView?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2121785/
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
How to center a ImageView vertically in its parent?
提问by user256239
My layout design is a TextView with long text and a ImageView to the right of the TextView. I want to center the ImageView in its parent vertically, so I used android:layout_centerVertical="true", but it turned out that the ImageView was aligned to the bottom of its parent. If I don't use layout_centerVertical property, the ImageView will be aligned to the top of its parent. How can I solve this problem? Thanks.
我的布局设计是一个带有长文本的 TextView 和一个位于 TextView 右侧的 ImageView。我想将 ImageView 在其父级垂直居中,所以我使用了 android:layout_centerVertical="true",但结果是 ImageView 与其父级的底部对齐。如果我不使用 layout_centerVertical 属性,则 ImageView 将与其父级的顶部对齐。我怎么解决这个问题?谢谢。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:background="#000000">
<ImageView android:id="@+id/t3" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:src="@drawable/arrow_right"
android:layout_alignParentRight="true" android:background="#ff0000"
android:layout_centerVertical="true" android:scaleType="centerInside" />
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="This a very very very very very very very very very very very very very very very very long sentence"
android:textColor="#231ACC" android:layout_toLeftOf="@id/t3" />
</RelativeLayout>
回答by JeremyFromEarth
You could use a LinearLayout
with layout_width
and layout_height
set to fill_parent
and gravity set to vertical_center
.
您可以使用LinearLayout
withlayout_width
和layout_height
setfill_parent
并将重力设置为vertical_center
。
Something like this:
像这样的东西:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical|center_horizontal"
android:orientation="horizontal">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/input_field"
android:gravity="left"
android:layout_weight="1"
android:text="Left Aligned"/>
<EditText
android:id="@+id/input_field"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:layout_weight="1"
android:text="Right Aligned"/>
</LinearLayout>
</LinearLayout>
回答by Pawan
change android:layout_height="wrap_content"
to android:layout_height="fill_parent"
of RelativeLayout and add android:layout_centerVertical="true"
to TextView, it will work like charm.
更改android:layout_height="wrap_content"
为android:layout_height="fill_parent"
RelativeLayout 并添加android:layout_centerVertical="true"
到 TextView,它将像魅力一样工作。
回答by Seshu Vinay
Try android:layout_gravity="center_vertical"
尝试 android:layout_gravity="center_vertical"
回答by papaiatis
This will put your image to the vertical center of it's parent:
这会将您的图像置于其父级的垂直中心:
android:layout_gravity="center_vertical"
android:layout_gravity="center_vertical"
Demo:
演示:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="64dp"
android:orientation="horizontal">
<ImageView
android:layout_width="48dip"
android:layout_height="48dip"
android:src="@drawable/ic_launcher"
android:contentDescription="Application icon"
android:layout_gravity="center_vertical" />
</LinearLayout>
回答by abriggs
Add this xml attribute to the child you wish to center within it's parent:
将此 xml 属性添加到您希望在其父项中居中的子项:
android:layout_centerInParent="true"
This will center it vertically/horizontally.
这将使其垂直/水平居中。
回答by cutez7boyz
Simply add this:
只需添加以下内容:
android:layout_gravity="center_vertical|center_horizontal"
android:layout_gravity="center_vertical|center_horizontal"