2012-08-12 49 views
2

我有一個聊天應用程序,我需要在listView中對齊左側或右側行。 我的問題是我無法左右對齊每一行。他們仍然左對齊。
我膨脹基於休耕代碼align android listView向左或向右行

奇數行

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/userprofile_view" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:background="@drawable/odd" 
android:layout_gravity="right"> 

<TextView 
    android:id="@+id/chat_message_odd" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:paddingLeft="10dp" 
    android:textColor="#66CD00" 
    /> 
</FrameLayout> 

每個佈局甚至

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/userprofile_view" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:background="@drawable/even" 
android:layout_gravity="left"> 

<TextView 
    android:id="@+id/chat_message" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:paddingLeft="10dp" 
    android:textColor="#000000" 
    /> 
</FrameLayout> 

主要佈局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" 
    android:background="#FFFFFF" > 


    <ListView 
    android:id="@+id/listMessages" 
    android:layout_width="wrap_content" 
    android:layout_height="0px" 
    android:layout_weight="1" 
    android:scrollbars="vertical" 
    android:divider="@null" 
    android:dividerHeight="0dp" /> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" > 
    <EditText 
     android:id="@+id/sendText" 
     android:layout_width="253dp" 
     android:layout_height="wrap_content" 
     android:autoText="false" 
     android:capitalize="none" 
     android:ems="10" 
     android:scrollHorizontally="true" 
     android:singleLine="true" 
     android:textSize="16sp" 
     android:hint="Enter text" 
     >  
    </EditText> 
    <Button 
     android:id="@+id/send" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"   
     android:text="Send" /> 

</LinearLayout> 

</LinearLayout> 

回答

2

使TextView的寬度match_parent和設置它的重力向右。

所以使它:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/userprofile_view" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:background="@drawable/odd" 
> 

<TextView 
    android:id="@+id/chat_message_odd" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:gravity="right" 
    android:textColor="#66CD00" 
    /> 
</FrameLayout> 

,或者如果你想有一個左對齊文本其作爲一個整體是其父的右側,我寧願去這樣的的RelativeLayout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/userprofile_view" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="@drawable/odd" > 



    <TextView 
     android:id="@+id/chat_message_odd" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:paddingLeft="10dp" 
     android:textColor="#66CD00" /> 

</RelativeLayout> 

***編輯 所以模擬列表視圖我只是做了一個垂直的LinearLayout這裏:

<RelativeLayout 
     android:id="@+id/userprofile_view" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" > 

     <TextView 
      android:id="@+id/chat_message_odd" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:background="@android:drawable/editbox_background" 
      android:gravity="right" 
      android:padding="10dp" 
      android:text="foo bar" 
      android:textColor="#66CD00" /> 
    </RelativeLayout> 

    <RelativeLayout 
     android:id="@+id/userprofile_view" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" > 

     <TextView 
      android:id="@+id/chat_message_even" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentRight="true" 
      android:background="@android:drawable/editbox_background" 
      android:gravity="right" 
      android:padding="10dp" 
      android:text="foo fighters" 
      android:textColor="#f400" /> 
    </RelativeLayout> 

</LinearLayout> 

上述代碼創建這樣一個觀點:

two lines of chat

+0

它不工作,它只對齊文本,並繪製仍然左對齊 – AlexGo 2012-08-12 14:35:18

+0

然後給TextView的背景繪製。 – 2012-08-12 14:38:24

+0

我試過這個,但我沒有解決問題 – AlexGo 2012-08-12 14:53:54

0

好嘗試這一個。

奇數行

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/userprofile_view" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:background="@drawable/odd" 
    android:layout_gravity="right"> 

<TextView 
    android:id="@+id/chat_message_odd" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:paddingLeft="10dp" 
    android:textColor="#66CD00" /> 
</FrameLayout> 

即使

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/userprofile_view" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:background="@drawable/even" 
android:layout_gravity="left"> 

<TextView 
    android:id="@+id/chat_message" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:paddingLeft="10dp" 
    android:textColor="#000000" /> 
</FrameLayout> 

它是否解決您的問題?

+0

它does'n工作,它只對齊文本和drawable仍然對齊左 – AlexGo 2012-08-12 14:42:27

+0

我修改了一下代碼。現在試試... – 2012-08-12 14:46:50

+0

我也試過,但沒有工作 – AlexGo 2012-08-12 14:50:29

相關問題