问题描述
我使用的是Android 1.5的列表视图来显示图像的列表和文字旁边的形象。我想垂直居中的文字,但文字是在该行,而不是中心的顶部。下面是我的布局:
I am using a list view in Android 1.5 to show a list of images and text next to the image. I am trying to vertically center the text but the text is at the top of the row instead of centered. Below is my layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/row"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dip">
<ImageView android:id="@+id/item_image" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:paddingRight="10dip"
android:src="@drawable/default_image" android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_centerVertical="true"
android:gravity="center_vertical"/>
<TextView android:id="@+id/item_title"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_toRightOf="@id/item_image"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_centerVertical="true"
android:gravity="center_vertical"
/>
</RelativeLayout>
这似乎很奇怪,我需要设置alignParentTop =真时,我想垂直居中的文本,但如果我不这么做的文字甚至不露面。我究竟做错了什么?
It seems strange that I need to set alignParentTop="true" when I'm trying to vertically center the text, but if I don't the text does not even show up. What am I doing wrong?
推荐答案
编辑如下的评论:
事实证明,使这项工作与RelativeLayout的是不容易的。在回答底部我已经包含了RelativeLayout的,让想要的效果,但只有等到它包含在一个ListView。在此之后,在这个问题中描述的同样的问题发生。这是固定的而不是使用的LinearLayout(S)。
It turns out making this work with RelativeLayout isn't easy. At the bottom of the answer I've included a RelativeLayout that gives the effect wanted, but only until it's included in a ListView. After that, the same problems as described in the question occurred. This was fixed by instead using LinearLayout(s).
<?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:paddingLeft="10dp"
android:orientation="horizontal">
<ImageView android:id="@+id/pickImageImage"
android:layout_width="100dp"
android:layout_height="80dp"
android:background="@drawable/icon"
android:scaleType="fitXY"
android:layout_marginRight="10dp"/>
<TextView android:id="@+id/pickImageText"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:gravity="left|center_vertical"
android:text="I'm the text"/>
</LinearLayout>
如果你想有两个文本框,你可以嵌套第二方向=垂直
和的LinearLayout ImageView的经过,然后把文本框在里面。
If you want to have two text boxes, you can nest a second orientation="vertical"
and LinearLayout after the ImageView and then put the text boxes in there.
这工作,但我不得不承认,我不知道为什么RelativeLayouts没有。例如,通过罗曼盖伊此博客文章明确表示,RelativeLayout的应该。当我试了一下,我从来没有得到它的相当工作;诚然,我没有做到这一点的完全的像他那样,但我唯一的变化是与TextViews的一些属性,应该不会做出太大的差别。
This works, but I have to admit I don't know why the RelativeLayouts didn't. For example, this blog post by Romain Guy specifically says that the RelativeLayout should. When I tried it, I never got it to quite work; admittedly I didn't do it exactly as he did, but my only changes were with some attributes of the TextViews, which shouldn't have made that much of a difference.
下面是原来的答案:
我想你混淆的Android与RelativeLayout的所有这些有点矛盾的指令。我重新格式化你的东西,以这样的:
I think you're confusing Android with all those somewhat contradictory instructions in RelativeLayout. I reformatted your thing to this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/row"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dip">
<ImageView android:id="@+id/item_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="10dip"
android:src="@drawable/icon"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"/>
<TextView android:id="@+id/item_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/item_image"
android:layout_centerVertical="true"
android:text="Blah!"/>
</RelativeLayout>
和工作正常。我删除了你的许多冗余的安卓layout_alignParentxxx
,因为它们是没有必要的。这一观点现在来了图片中的左上角,垂直居中旁边的文本。如果你想要的图片垂直居中为好,那么你就不能有RelativeLayout的是Android上:layout_height =WRAP_CONTENT,因为它是使出浑身解数没有比图片的高度更高。你必须指定一个高度,如80dp,然后设置ImageView的像60dp与Android固定高度:scaleType =fitXY,使其缩小以适合正常
And that works fine. I removed many of your redundant android:layout_alignParentxxx
because they weren't necessary. This view now comes up with the picture in the top left corner and the text vertically centered next to it. If you want the picture vertically centered as well, then you can't have the RelativeLayout be on android:layout_height="wrap_content" because it's trying to make itself no taller than the height of the picture. You'd have to specify a height, e.g. 80dp, and then set the ImageView to a fixed height like 60dp with android:scaleType="fitXY" to make it scale down to fit properly.
这篇关于我如何垂直对齐内使用相对布局列表中的项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!