0

嘿,我正在做一个简单的聊天应用程序。我在这两个布局,以通为充气添加到RecyclerView:LinearLayout重力里面RecyclerView

received_msg.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:background="#f00"> 

<TextView 
    android:text="Hello" 
    android:textColor="#fff" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" /> 

</LinearLayout> 

sent_msg.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:background="#fff" 
    android:layout_gravity="right"> 

<TextView 
    android:text="Text" 
    android:textColor="#f00" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" /> 

</LinearLayout> 

RecyclerView:

<android.support.v7.widget.RecyclerView 
    android:padding="10dp" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:scrollbarStyle="outsideOverlay" 
    android:scrollbars="vertical" /> 

layout_gravity属性似乎对sent_msg.xml的LinearLayout没有影响。该怎么办?

回答

0

可以使两个项目XML的全宽(match_parent)和移动layout_gravitybackground属性对孩子的TextView:

sent_msg.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent"     (* Changed) 
    android:layout_height="wrap_content"> 

<TextView 
    android:text="Text" 
    android:background="#fff"       (* Changed) 
    android:textColor="#f00" 
    android:layout_gravity="right"      (* Changed) 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" /> 

</LinearLayout> 
+0

哈哈,对不起,老兄你是正确的。谢谢! –

相关问题