android - android listView 行向左或向右对齐

标签 android android-layout

我有一个聊天应用程序,我需要在 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>

最佳答案

使 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>

*** 编辑 所以模拟 ListView 我只是在这里做了一个垂直的 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>

上面的代码创建了一个像这样的 View :

two lines of chat

关于android - android listView 行向左或向右对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11922916/

相关文章:

android - 使用自定义图像作为按钮来安排布局

android - 从 ScrollView 中的元素更改 z 顺序

android - BottomSheetBehavior 与两个 RecyclerView

android - 无法通过 Firebase 获取用户的 Gmail 电子邮件地址

android - 约束布局 - 组可见性在动态模块内不起作用

android - 如何使用 Tag 代替 R.id。在自定义 ArrayAdapter 的构造函数中?

java - 模糊展开的 RecyclerView 项目的周围(模糊 RecyclerView 除了其中一项)

android - 在android中使用charAt(index)

android - 什么是 android.jar ?它包括什么?

java - Exchange Service 泄露了 ServiceConnection、GridView Adapter