如何垂直对齐使用相对布局列表中的项目?
-
20-09-2019 - |
题
我正在使用机器人1.5列表视图,以显示图像的旁边的图像列表和文本。我想垂直居中的文本,但文本是在行,而不是居中的顶部。下面是我的布局:
<?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 =“真”当我试图垂直居中的文本,但如果我不文本甚至不露面。我在做什么错了?
解决方案
EDIT以下评价:
原来使用的RelativeLayout这项工作是不容易的。在回答底部我已经包含了RelativeLayout的,让想要的效果,但直到它包含在一个ListView。在这之后,如问题描述的同样的问题发生。这是通过固定而不是使用的LinearLayout(一个或多个)。
<?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>
如果你想有两个文本框,你可以嵌套第二orientation="vertical"
和LinearLayout中的ImageView的之后,然后把文本框在里面。
这工作,但我不得不承认,我不知道为什么RelativeLayouts没有。例如,这博客文章由罗曼盖伊具体说,应该RelativeLayout的。当我试了一下,我从来没有它得到相当的工作;诚然,我没有做到这一点的究竟的像他那样,但我唯一的变化是与TextViews的一些属性,不应该做出多大的差别这一点。
下面是原来的答案:
我觉得你在RelativeLayout的所有这些有点矛盾的指示混乱的Android。我重新格式化你的事这样的:
<?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>
和工作正常。我删除了你的很多冗余android:layout_alignParentxxx
的,因为他们是没有必要的。现在这种观点在左上角的画面和垂直居中旁边的文字出现。如果你想要的图片垂直居中为好,那么你就不能有RelativeLayout的是Android上:layout_height =“WRAP_CONTENT”,因为它试图使自己没有比图片的高度更高。你必须指定一个高度,例如80dp,然后将ImageView的设置为一个固定的高度等与机器人60dp:scaleType =“fitXY”,使其缩小,以适应正确
其他提示
被困在了一阵类似的问题,但是发现这个从CommonsWare:
“当你膨胀的布局,使用inflate(R.layout.whatever, parent, false)
,其中亲本是ListView中。”
作品,但只有当你的行的高度设置为特定值(即,不能使用WRAP_CONTENT)。
基线指令将做到这一点,但ImageView的根本不支持基线对齐今天的。你可以解决此通过创建的ImageView的一个子类,重写getBaseline均()方法和返回图像的高度。