繁体   English   中英

在Android布局中居中对齐TextView

[英]Center align TextView in 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>

您可以将权重分配给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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM