[英]Center TextView directly below ImageView in Linear Layout
I am trying to align text directly below each icon despite the text length being of different sizes. 我试图在每个图标正下方对齐文本,尽管文本长度不同。 I'd prefer for everything to be centered on this line in this screenshot: 我希望此屏幕截图中的所有内容都集中在此行上:
I have tried using both Linear and Relative layouts to get what I want with no success. 我尝试同时使用线性和相对布局来获得想要的结果,但没有成功。 This code is what I have now and is the closest to what I want: 这段代码是我现在拥有的,并且与我想要的最接近:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=
"http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp" >
<ImageView
android:id="@+id/grid_item_image"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginRight="10dp"
android:src="@drawable/football_logo" >
</ImageView>
<TextView
android:id="@+id/grid_item_label"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@+id/label"
android:layout_weight="1"
android:layout_marginTop="5dp"
android:gravity="center"
android:textSize="15sp">
</TextView>
</LinearLayout>
Surely there is a better way to do this (Maybe a RelativeLayout with a LinearLayout around the Textview?). 当然,有更好的方法可以做到这一点(也许是在Textview周围带有LinearLayout的RelativeLayout?)。 Any help would be appreciated... I'm fairly new to this. 任何帮助将不胜感激...我对此还很陌生。
If your icons are sized correctly (eg ~48 pixels in the drawable-mdpi folder) then you can dispense with the ImageView
altogether and use android:drawableTop="@drawable/football_logo"
in the TextView
. 如果您的图标大小正确(例如drawable-mdpi文件夹中的〜48像素),则可以完全省去ImageView
并在TextView
使用android:drawableTop="@drawable/football_logo"
。
If you want to stick with your nested layouts approach, then use android:layout_gravity="center_horizontal"
in the ImageView
to center the ImageView
horizontally. 如果您想坚持使用嵌套布局方法,请在ImageView
使用android:layout_gravity="center_horizontal"
将ImageView
水平居中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.