相对布局中如何使两个文本视图占据高度的50%

4

我有一个自定义的列表视图,其中包含以下项目布局:

<?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="?android:attr/listPreferredItemHeight"
    android:paddingBottom="2dip"
    android:paddingTop="2dip" >

    <ImageView
        android:id="@+id/icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentTop="true"
        android:paddingLeft="6dip" />

    <TextView
        android:id="@android:id/text1"
        android:layout_width="50sp"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@id/icon"
        android:ellipsize="marquee"
        android:gravity="center"
        android:paddingLeft="6dip"
        android:singleLine="true"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/start_point"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@android:id/text1"
        android:ellipsize="marquee"
        android:paddingLeft="6dip"
        android:paddingRight="6dip"
        android:singleLine="true"
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <TextView
        android:id="@+id/end_point"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_below="@id/start_point"
        android:layout_toRightOf="@android:id/text1"
        android:ellipsize="marquee"
        android:gravity="center_vertical"
        android:paddingLeft="6dip"
        android:paddingRight="6dip"
        android:singleLine="true"
        android:textAppearance="?android:attr/textAppearanceSmall" />

</RelativeLayout>

除了 start_pointend_point 的高度不相等之外,它基本上显示了我想要的样子。我尝试了很多方法,但要么语法错误,要么布局混乱。有没有可行的解决方案来实现这一点?

1个回答

5

除了起点和终点在高度上不相等之外,它基本符合我的要求。我尝试了很多方法,但要么出现语法错误,要么布局混乱。有没有可行的解决方案可以实现这一点?

在两个TextViews之前的布局中插入一个额外的视图,就像这样,它将作为一个锚点来为TextViews提供相等的空间:

<RelativeLayout>
    // the icon and the first TextView here

    <View android:id="@+id/anchor" android:layout_centerVertical="true" android:layout_alignParentRight="true"      
        android:layout_width="2dp"
        android:layout_height="0dp"/>

    // the two TextView below it
   <TextView
       android:id="@+id/start_point" android:layout_above="@id/anchor" // the rest of the attributes />

   <TextView
       android:id="@+id/end_point" android:layout_below="@id/anchor" // the rest of the attributes />
</RelativeLayout>

1
太好了,谢谢。一个注释:使用layout_centerVertical而不是centerVertical - Andrey Novikov

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接