0

我需要实现固定的中心对齐,如图所示:

http://postimage.org/image/t735us1z3/

因此,标志图像始终位于中心,而两个 TextView 向左和向右增长。因此,无论文本有多长,图像仍将位于中心。再加上中间的“vs”TextView。

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal"
 >

<TextView android:id="@+id/list_item_entry_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:singleLine="true"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:ellipsize="marquee"
        android:fadingEdge="horizontal"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="5dp"
         />



     <ImageView 
        android:id="@+id/flag1"
        android:layout_width="20dp"
        android:layout_height="20dp"   
        android:layout_marginLeft="5dp" 
        android:layout_marginRight="5dp" 
        android:src="@drawable/bayern25"
        android:layout_marginTop="5dp" 
      android:layout_toRightOf="@+id/list_item_entry_title"
        />

       <TextView android:text="-"
             android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:singleLine="true"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:ellipsize="marquee"
        android:fadingEdge="horizontal"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="5dp"
         android:layout_toRightOf="@+id/flag1"
         />


       <ImageView 
        android:id="@+id/flag2"
        android:layout_width="20dp"
        android:layout_height="20dp"  
        android:layout_marginLeft="5dp"  
          android:layout_marginTop="5dp" 
        android:src="@drawable/wolfsburg25"
        android:layout_toRightOf="@+id/text"
        />


     <TextView android:id="@+id/list_item_entry_summary"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/list_item_entry_title"
        android:layout_alignLeft="@id/list_item_entry_title"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:singleLine="true"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="5dp"
        android:layout_toRightOf="@+id/flag2"
         />

4

2 回答 2

1

由于您使用的是 RelativeLayout,因此您只需使用 layout_centerInParent 属性 (android:layout_centerInParent="true") 将“vs”TextView 定位在中心,然后相对于该组件定位其他组件。你的代码看起来像

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <TextView
        android:id="@+id/list_item_entry_title"
        android:layout_toLeftOf="@+id/flag1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="5dp"
        android:layout_marginTop="5dp"
        android:ellipsize="marquee"
        android:fadingEdge="horizontal"
        android:singleLine="true"
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <ImageView
        android:id="@id/flag1"
        android:layout_toLeftOf="@+id/text"
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:layout_marginTop="5dp"
        android:src="@drawable/bayern25" />

    <TextView
        android:id="@id/text"
        android:layout_centerInParent="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="5dp"
        android:layout_marginTop="5dp"
        android:layout_toRightOf="@+id/flag1"
        android:ellipsize="marquee"
        android:fadingEdge="horizontal"
        android:singleLine="true"
        android:text="-"
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <ImageView
        android:id="@+id/flag2"
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="5dp"
        android:layout_toRightOf="@id/text"
        android:src="@drawable/wolfsburg25" />

    <TextView
        android:id="@+id/list_item_entry_summary"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@id/list_item_entry_title"
        android:layout_below="@id/list_item_entry_title"
        android:layout_marginBottom="5dp"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="5dp"
        android:layout_toRightOf="@id/flag2"
        android:singleLine="true"
        android:textAppearance="?android:attr/textAppearanceSmall" />
于 2012-09-18T19:19:43.447 回答
0

您可以使用 LinearLayout 重量和重力来实现这一点,如下所示:

<LinearLayout
    android:width="match_parent"
    android:height="wrap_content"
    android:orientation="horizontal">
    <TextView
        android:width="0dp"
        android:weight="1"
        android:height="wrap_content"
        android:gravity="right"
        android:text="Team 1" />
    <TextView
        android:width="wrap_content"
        android:height="wrap_content"
        android:text="VS" />
    <TextView
        android:width="0dp"
        android:weight="1"
        android:height="wrap_content"
        android:gravity="left"
        android:text="Team 2" />
</LinearLayout>
于 2012-09-18T18:59:35.990 回答