中心在Android布局中对齐TextView

时间:2015-04-29 01:24:58

标签: android layout

这就是我想要实现的目标,而我却没有得到它。

 This is a long lable: {image1}{image1}
          Short label: {image1}{image1}
   Other label length: {image1}{image1}

请注意,标签末端的冒号是对齐的,并且尺寸相同的图像也将对齐。不同长度的标签不会左对齐。实际上的标签都只是一个单词,所以空间应该是个问题,但单词的宽度不一样。

下面的布局集中了所有内容,但它们就是这样出来的。

      This is a long lable: {image1}{image1}
          Short label: {image1}{image1}
       Other label length: {image1}{image1}

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:paddingTop="2dp"
    android:paddingBottom="2dp"
    android:id="@+id/layout_one">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="22sp"
        android:text="@string/image_one"
        android:id="@+id/one"/>
<ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/digit_border"
        android:padding="2dp"
        android:adjustViewBounds="true"
        android:id="@+id/one1"
        android:src="@drawable/five"
        android:scaleType="centerCrop"/>        
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/digit_border"
        android:padding="2dp"
        android:adjustViewBounds="true"
        android:id="@+id/one2"
        android:src="@drawable/five"
        android:scaleType="centerCrop"/>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:id="@+id/layout_days"
    android:layout_below="@+id/layout_two"
    android:gravity="center"
    android:paddingTop="2dp"
    android:paddingBottom="2dp">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="22sp"
        android:text="@string/image_two"
        android:id="@+id/two"/>
<ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/digit_border"
        android:padding="2dp"
        android:adjustViewBounds="true"
        android:id="@+id/two1"
        android:src="@drawable/five"
        android:scaleType="centerCrop"/>
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/digit_border"
        android:padding="2dp"
        android:adjustViewBounds="true"
        android:id="@+id/two2"
        android:src="@drawable/five"
        android:scaleType="centerCrop"/>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:id="@+id/layout_three"
    android:layout_below="@+id/layout_days"
    android:gravity="center"
    android:paddingTop="2dp"
    android:paddingBottom="2dp">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="22sp"
        android:text="@string/image_three"
        android:id="@+id/three"/>
<ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/digit_border"
        android:padding="2dp"
        android:adjustViewBounds="true"
        android:id="@+id/three1"
        android:src="@drawable/five"
        android:scaleType="centerCrop"/>
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/digit_border"
        android:padding="2dp"
        android:adjustViewBounds="true"
        android:id="@+id/hour2"
        android:src="@drawable/three2"
        android:scaleType="centerCrop"/>
</LinearLayout>

1 个答案:

答案 0 :(得分:1)

您可以将权重分配给textView并将重力设置为右侧,这样它们将为所有三行占用相同的空间,右侧的重力将确保文本右对齐。下面是您可以根据情况设置重量的布局文件。

  

具有基本布局的第一个XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:paddingBottom="2dp"
    android:paddingTop="2dp" >

    <TextView
        android:id="@+id/one"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.2"
        android:gravity="right|center_vertical"
        android:text="text1 : "
        android:textSize="22sp" />

    <ImageView
        android:id="@+id/one1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.4"
        android:adjustViewBounds="true"
        android:padding="2dp"
        android:scaleType="centerCrop"
        android:src="@drawable/ic_launcher" />

    <ImageView
        android:id="@+id/one2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.4"
        android:adjustViewBounds="true"
        android:padding="2dp"
        android:scaleType="centerCrop"
        android:src="@drawable/ic_launcher" />
</LinearLayout>
  

创建完整视图的第二个XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:paddingBottom="2dp"
    android:paddingTop="2dp" >

    <include
        android:id="@+id/layout_one"
        layout="@layout/test_item" />

    <include
        android:id="@+id/layout_two"
        layout="@layout/test_item" />

    <include
        android:id="@+id/layout_three"
        layout="@layout/test_item" />

</LinearLayout>
相关问题