簡體   English   中英

Android - 隨機文本的TextView對齊方式

[英]Android - TextView alignment for random text

我正在開發一個應用程序。 因為我從webservice獲取數據,我將其插入TextView 但是,textview的對齊正在變得混亂。 請檢查圖像。

前3行(黑色)按正確順序排列,從4號開始加擾。 我搜索了很多次,但沒有得到我想要的確切解決方案。

在此輸入圖像描述

XML代碼:

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

     <TextView
        android:id="@+id/schedule_1_textView_day_date"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="KKR"
        android:layout_weight="1.5"
        android:layout_marginTop="10dp"
        android:textColor="@color/Black"            
        android:textSize="7sp"
        />        

    <TextView
        android:id="@+id/schedule_1_textView_matchno"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="10"
        android:layout_weight="1.5"
        android:layout_marginTop="10dp"
        android:textColor="@color/Black"
        android:textSize="7sp"
        />        

    <TextView
        android:id="@+id/schedule_1_textView_time"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_weight="1"
        android:text="6"
        android:textColor="@color/Black"
        android:textSize="7sp"
        />   

    <TextView
        android:id="@+id/schedule_1_textView_team1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_weight="1"
        android:text="4"
        android:textSize="7sp"
        android:textColor="@color/Black"
        />       

    <TextView
        android:id="@+id/schedule_1_textView_team2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="0"
        android:layout_weight="1"
        android:layout_marginTop="10dp"          
        android:textColor="@color/Black"
        android:textSize="7sp"
        />       

    <TextView
        android:id="@+id/schedule_1_textView_venue"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="6"
        android:layout_marginTop="10dp"
        android:textColor="@color/Black"
        android:textSize="7sp"
        /> 

    <TextView
        android:id="@+id/schedule_1_textView_blank"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="B"
        android:layout_marginTop="20dp"
        android:textColor="@color/Black"
        android:textSize="6sp"
        android:visibility="invisible"
        /> 


 </LinearLayout>

你可能想把android:layout_weight屬性告訴TextView如:

<TextView
    android:id="@+id/schedule_1_textView_venue"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="6"
    android:layout_marginTop="10dp"
    android:textColor="@color/Black"
    android:textSize="7sp" />

你需要根據你的要求分配重量 即對於較大的視圖,您希望分配更高的值。

[編輯]和最后一個TextView你可能想設置android:layout_gravity="center|right"android:layout_gravity="center|left"為第一個用於文本對齊的其余部分你應該使用android:layout_gravity="center"

嘗試這個..

你忘了添加android:layout_weight="1"為空兩TextView使用android:singleLine="true"為所有TextView

<TextView
    android:id="@+id/schedule_1_textView_venue"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:singleLine="true"
    android:text="6"
    android:layout_marginTop="10dp"
    android:textColor="@color/Black"
    android:textSize="7sp"
    /> 

<TextView
    android:id="@+id/schedule_1_textView_blank"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:singleLine="true"
    android:text="B"
    android:layout_marginTop="20dp"
    android:textColor="@color/Black"
    android:textSize="6sp"
    android:visibility="invisible"
    /> 

將重力添加到TextView如:

android:gravity="center_vertical|center_horizontal"

根據需要將重力改為左,右或中心......

你有沒有嘗試為schedule_1_textView_venue添加權重?

這是一個更多的工作,但你嘗試使用TableLayout 是非常有用的教程,可以幫助您組織正確的視圖順序。

編輯:我想我知道是什么導致TextViews不匹配。 它來自您上一列名為VENUE的值的不同長度。 就拿值HYDEBARAD德里 -第一個比第二要長得多,這使得所有的TextViews該行向左走。 我相信如果你在TextViews相同的padding ,你將對齊它們。 例如:在MATCH列中取出TextViews並編寫以下內容: myTextView.setPadding(40, 0, 0, 0) (左側padding )這將使它們完全成直線。 使用正確的padding值對其余的TextViews執行相同的操作,考慮其他列中的值的長度......但這有點棘手。 :)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM