如何均匀分布不同长度的文本视图?

时间:2014-06-30 09:49:44

标签: android android-layout android-listview textview android-layout-weight

我想在listView中展开不同长度的文本视图。 这是我到目前为止所得到的:

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

    <TextView
        android:id="@+id/dateTextView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_marginTop="5dp"
        android:adjustViewBounds="true"
        android:gravity="center_horizontal|center_vertical"
        android:text="@string/empty_int"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <View
        android:layout_width="2dp"
        android:layout_height="match_parent"
        android:layout_marginBottom="0dp"
        android:layout_marginTop="0dp"
        android:background="#EF6C00" >
    </View>

我想要实现的布局是这样的:

date | time | score | distance | altitude  

数字:

dd/mm/yyyy | hh:mm:ss | [0-1000] | [0-100] | [0-100]

10 spaces | 8 spaces | 4 spaces | 3 spaces | 3spaces

这就是它现在的样子,可悲的是:

2 个答案:

答案 0 :(得分:3)

您可以使用Android的weight属性

  

10个空格| 8个空格| 4个空格| 3个空格| 3spaces

因此10 + 8 + 4 + 3 + 3(对于TextView)+ 4(对于View)= 32

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="32">

    <TextView
        android:id="@+id/dateTextView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="10"
        android:layout_marginTop="5dp"
        android:adjustViewBounds="true"
        android:gravity="center_horizontal|center_vertical"
        android:text="@string/empty_int"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <View
        android:layout_width="2dp"
        android:layout_weight="1"
        android:layout_height="match_parent"
        android:layout_marginBottom="0dp"
        android:layout_marginTop="0dp"
        android:background="#EF6C00" >

     <TextView
        android:id="@+id/dateTextView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="8"
        android:layout_marginTop="5dp"
        android:adjustViewBounds="true"
        android:gravity="center_horizontal|center_vertical"
        android:text="@string/empty_int"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <View
        android:layout_width="2dp"
        android:layout_weight="1"
        android:layout_height="match_parent"
        android:layout_marginBottom="0dp"
        android:layout_marginTop="0dp"
        android:background="#EF6C00" >

    <TextView
        android:id="@+id/dateTextView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="4"
        android:layout_marginTop="5dp"
        android:adjustViewBounds="true"
        android:gravity="center_horizontal|center_vertical"
        android:text="@string/empty_int"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <View
        android:layout_width="2dp"
        android:layout_weight="1"
        android:layout_height="match_parent"
        android:layout_marginBottom="0dp"
        android:layout_marginTop="0dp"
        android:background="#EF6C00" >

    <TextView
        android:id="@+id/dateTextView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="3"
        android:layout_marginTop="5dp"
        android:adjustViewBounds="true"
        android:gravity="center_horizontal|center_vertical"
        android:text="@string/empty_int"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <View
        android:layout_width="2dp"
        android:layout_weight="1"
        android:layout_height="match_parent"
        android:layout_marginBottom="0dp"
        android:layout_marginTop="0dp"
        android:background="#EF6C00" >

    <TextView
        android:id="@+id/dateTextView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="3"
        android:layout_marginTop="5dp"
        android:adjustViewBounds="true"
        android:gravity="center_horizontal|center_vertical"
        android:text="@string/empty_int"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    </LinearLayout>

答案 1 :(得分:2)

试试这个:

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

    <TextView
        android:id="@+id/dateTextView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:layout_marginTop="5dp"
        android:adjustViewBounds="true"
        android:gravity="center_horizontal|center_vertical"
        android:text="@string/empty_int"
        android:textAppearance="?android:attr/textAppearanceMedium" />
<View
        android:layout_width="2dp"
        android:layout_height="match_parent"
        android:layout_marginBottom="0dp"
        android:layout_marginTop="0dp"
        android:background="#EF6C00" >
    </View>
 <TextView
        android:id="@+id/timeTextView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="4"
        android:layout_marginTop="5dp"
        android:adjustViewBounds="true"
        android:gravity="center_horizontal|center_vertical"
        android:text="@string/empty_int"
        android:textAppearance="?android:attr/textAppearanceMedium" />
<View
        android:layout_width="2dp"
        android:layout_height="match_parent"
        android:layout_marginBottom="0dp"
        android:layout_marginTop="0dp"
        android:background="#EF6C00" >
    </View>
 <TextView
        android:id="@+id/scoreTextView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="6"
        android:layout_marginTop="5dp"
        android:adjustViewBounds="true"
        android:gravity="center_horizontal|center_vertical"
        android:text="@string/empty_int"
        android:textAppearance="?android:attr/textAppearanceMedium" />
<View
        android:layout_width="2dp"
        android:layout_height="match_parent"
        android:layout_marginBottom="0dp"
        android:layout_marginTop="0dp"
        android:background="#EF6C00" >
    </View>
 <TextView
        android:id="@+id/distanceTextView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="10"
        android:layout_marginTop="5dp"
        android:adjustViewBounds="true"
        android:gravity="center_horizontal|center_vertical"
        android:text="@string/empty_int"
        android:textAppearance="?android:attr/textAppearanceMedium" />
<View
        android:layout_width="2dp"
        android:layout_height="match_parent"
        android:layout_marginBottom="0dp"
        android:layout_marginTop="0dp"
        android:background="#EF6C00" >
    </View>
 <TextView
        android:id="@+id/altitudeTextView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="10"
        android:layout_marginTop="5dp"
        android:adjustViewBounds="true"
        android:gravity="center_horizontal|center_vertical"
        android:text="@string/empty_int"
        android:textAppearance="?android:attr/textAppearanceMedium" />

</LinearLayout>