2014-12-05 103 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>