2014-12-05 99 views
1

我有一個LinearLayout中的textView,它顯示的文本不總是居中,即使我使用gravity =「center」屬性。此外,我已經設置了layou_width與「match_parent。TextView重力屬性不起作用

這TextView的是在的LinearLayout這在LinearLayout中這是在考慮到父母的LinearLayout。

我TextView的是在ListView和代表結束該項目生產線。

我的適配器使用ViewHolder模式。

在這裏,我真不明白,爲什麼有時文本中心和有時它的左側。

這裏2個截圖我的觀點的wi TH不同的狀態,你可以看到對齊問題:

example1

example2

example3

我想吃點什麼: 這只是對文字現在或者是時候了顯示在這個地方,我想只顯示在中心這個文本,有時它顯示在左邊,你可以看到上面的截圖。

good result

ListView的佈局:

<ListView 
        android:id="@+id/list" 
        android:layout_width="match_parent" 
        android:layout_height="fill_parent" 
        android:layout_alignParentLeft="true" 
        android:layout_alignParentRight="true" 
        android:layout_below="@+id/dialogTitle" 
        android:listSelector="@drawable/listitem_selector" 
        tools:listitem="@layout/item_customer_in_queue"> 
       </ListView> 

項目佈局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:id="@+id/item_in_queue_list" 
       android:layout_width="match_parent" 
       android:layout_height="@dimen/in_queue_item_height" 
       android:minHeight="@dimen/in_queue_item_height" 
       android:background="@color/bckg" 
       android:orientation="vertical"> 

    <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="0.95"> 

     <RelativeLayout 
       android:layout_width="wrap_content" 
       android:layout_height="fill_parent"> 

      <ImageView.../> 
      <ImageView.../> 
      <ImageView.../> 

     </RelativeLayout> 
     <RelativeLayout 
       android:layout_width="wrap_content" 
       android:layout_height="fill_parent"> 

      <ImageView.../> 
      <ImageView.../> 

     </RelativeLayout> 

     <TextView.../> 
     <TextView.../> 
     <TextView.../> 

     <LinearLayout         <=here my LinearLayout of my issue 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:orientation="vertical"> 
      <View 
        android:layout_width="fill_parent" 
        android:layout_height="20dp" 
        android:layout_weight="0.1" 
        android:layout_gravity="bottom"/> 
      <TextView 
        android:id="@+id/list_dueTime" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:gravity="center"     <=here my issue 
        android:text="due" 
        android:layout_weight="0.8" 
        android:textColor="@color/dark_grey" 
        android:textAppearance="?android:attr/textAppearanceSmall" /> 
     </LinearLayout> 

    </LinearLayout> 

    <ProgressBar.../> 

</LinearLayout> 
+0

U可以只顯示烏爾問題? – Android 2014-12-05 12:12:30

+0

'layout_gravity =「bottom」'對於父'LinearLayout''orientation =「vertical」不起作用'' – kId 2014-12-05 12:12:38

+0

'你試過了layout_gravity =「center」? – Umair 2014-12-05 12:14:53

回答

3

試試這個: 在這種情況下,我所提供的重力文本視圖的父,並改變了文本查看寬度以包裝內容 。 。 。 。 。 。

<LinearLayout         
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
       android:gravity="center" 
      android:orientation="vertical"> 
     <View 
       android:layout_width="fill_parent" 
       android:layout_height="20dp" 
       android:layout_weight="0.1" 
       android:layout_gravity="bottom"/> 
     <TextView 
       android:id="@+id/list_dueTime" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="due" 
       android:layout_weight="0.8" 
       android:textColor="@color/dark_grey" 
       android:textAppearance="?android:attr/textAppearanceSmall" /> 
    </LinearLayout> 

。 。 。 。

+0

謝謝,但不幸的是它不起作用 – AlexDG 2014-12-05 12:23:53

0

當你在談論對齊「中心」時,中心由視圖的寬度決定。爲了幫助您進行調試,您可以向視圖添加彩色背景並查看其邊界。

0

做一個佈局XML和粘貼下面的代碼

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:padding="5dp" > 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="80dp" 
    android:weightSum="1" > 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_marginRight="5dp" 
     android:layout_weight="0.4" 
     android:background="#f0f0f0" > 

    </RelativeLayout> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_weight="0.6" 
     android:background="#f0f0f0" 
     android:layout_height="match_parent" > 

     <TextView 
      android:id="@+id/txt_now" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentLeft="true" 
      android:layout_centerVertical="true" 
      android:layout_marginLeft="5dp" 
      android:textSize="18sp" 
      android:text="Now" /> 

     <TextView 
      android:id="@+id/textView1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentBottom="true" 
      android:layout_alignParentLeft="true" 
      android:layout_marginLeft="5dp" 
      android:text="12 Mins Overdue" 
      android:textAppearance="?android:attr/textAppearanceSmall" /> 

    </RelativeLayout> 

</LinearLayout>