简体   繁体   English

在Android布局中居中对齐TextView

[英]Center align TextView in android layout

This is what I'm trying to achieve and I'm just not getting it. 这是我想要实现的目标,但我没有做到。

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

Notice that the colons at the ends of the labels are aligned and the images, which are the same dimensions, will also be aligned. 请注意,标签末端的冒号已对齐,并且相同尺寸的图像也将对齐。 The labels of varying lengths will not be left aligned. 不同长度的标签将不会对齐。 The labels in reality are all just one word, so space should be be an issue, but the words are not the same widths. 现实中的标签都只是一个单词,因此空间应该是一个问题,但是单词的宽度并不相同。

The layout below centers everything but they come out like this. 下面的布局将所有内容居中,但它们以这种方式出现。

      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>

You can assign the weight to your textView and set the gravity to right that way they will take same space for all three rows and gravity right will make sure text is right aligned. 您可以将权重分配给textView并将权重设置为正确,这样它们将为所有三行占据相同的空间,而向右的重力将确保文本正确对齐。 Below are the layout files you can set the weight as it works in your case. 下面是布局文件,您可以根据自己的情况设置权重。

First XML which has basic layout 第一个具有基本布局的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>

Second XML which create full view 创建完整视图的第二个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