簡體   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