2014-04-01 114 views
0

我正在開發一個應用程序。在這我從Web服務獲取數據,我將它插入TextView。但是,textview的排列正在變得混亂。請檢查圖像。Android - 隨機文本的TextView對齊方式

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

enter image description here

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> 
+0

處處設置layout_width到' 「0」' –

回答

1

試試這個..

你忘了爲空兩TextView使用android:singleLine="true"添加android:layout_weight="1"所有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" 
    /> 
+0

現在仍在工作.. –

0

加入重力您TextView像:

android:gravity="center_vertical|center_horizontal" 

改變重力左,右或中心,你需要...

+0

所有的TextView? –

+0

剛剛嘗試..同樣的問題.. –

+0

我覺得你正在爲你的數據獲取空間,最好在添加到TextView之前嘗試修剪它們... – Vamshi

0

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

+0

是的。但仍然是同樣的問題 –

+0

你應該嘗試使用TableLayout而不是LinearLayout,然後,這些列應該對齊你的文字瀏覽 –

+0

仍然在工作.. –

1

你可能assigh 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"

+0

試過。同一問題 –

+0

這可能是因爲您的文本包含多餘的空格。你可以通過使用'.trim()'方法來解決這個問題,在文本的開頭和結尾刪除額外的空格。 – SMR

+0

仍然在工作.. –

0

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

編輯:我想我知道是什麼導致了TextViews的不匹配。它來自您最後一列VENUE中不同長度的值。舉例來說,值爲HYDEBARADDELHI - 第一個比第二個長得多,這使得行上的所有TextViews都離開了。我相信,如果您將相同的padding添加到TextViews,您將對齊它們。例如:在MATCH列中輸入TextViews列,然後寫入以下內容:myTextView.setPadding(40, 0, 0, 0)padding在左側),這將使它們完全排成一排。與其他TextViews一樣,使用正確的padding值,考慮其他列中值的長度......但這有點棘手。 :)

+0

仍然在工作.. –

+0

檢查我的編輯:) – taxo

+0

@Mitesh你試過我的解決方案嗎? (: – taxo