【问题标题】:How to make list view items align left and right programmatically in Android如何在Android中以编程方式使列表视图项目左右对齐
【发布时间】:2017-01-25 11:49:08
【问题描述】:

我正在开发一个聊天应用程序,在聊天视图中我必须左右显示消息。但由于我在 Android 编程方面还很陌生,所以我无法做到这一点。这是我得到的:

这是我用来显示订单项的 chatbubble.xml。

    <?xml version="1.0" encoding="utf-8"?>
        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/bubble_layout_parent"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <LinearLayout
                android:id="@+id/bubble_layout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/bg_msg1">

                <TextView
                    android:id="@+id/message_text"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:maxEms="12"
                    android:layout_gravity="center"
                    android:text="Hi! new message"
                    android:textColor="@android:color/primary_text_light" />
            </LinearLayout>

        </LinearLayout>

ChatAapter.java的getView方法

    @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = layoutInflater.inflate(resource, parent, false);

            Holder holder = new Holder();
            holder.txtMsg = (TextView) view.findViewById(R.id.message_text);

            HashMap<String,String> hashMap = new HashMap<String,String>();
            hashMap = chats.get(position);

            LinearLayout layout = (LinearLayout) view.findViewById(R.id.bubble_layout);
            LinearLayout parent_layout = (LinearLayout) view.findViewById(R.id.bubble_layout_parent);

            layout.setPadding(20,20,20,20);

            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
            params.setMargins(0,0,0,20);
            layout.setLayoutParams(params);

            if(hashMap.get("is_mine").equals("yes")) {
                layout.setBackgroundResource(R.drawable.bg_msg1);
                parent_layout.setGravity(Gravity.RIGHT);
            } else {
                layout.setBackgroundResource(R.drawable.bg_msg2);
                parent_layout.setGravity(Gravity.LEFT);
            }

            holder.txtMsg.setText(hashMap.get("message"));
            holder.txtMsg.setTextColor(Color.BLACK);

            return view;
        }

这里是activity_chat_list.xml,它是与适配器一起使用的主列表视图文件。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.mychatapp.UserChat">

    <ListView
        android:id="@+id/msgListView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/form"
        android:divider="@null"
        android:dividerHeight="0dp"
        android:paddingBottom="10dp"
        android:text="Hello World" />

    <LinearLayout
        android:id="@+id/form"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:background="#91f1f1f1"
        android:paddingBottom="2dp">

        <EditText
            android:id="@+id/messageEditText"
            android:layout_width="252dp"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:layout_toLeftOf="@+id/sendMessageButton"
            android:layout_weight="0.72"
            android:ems="10"
            android:maxHeight="80dp" />

        <ImageButton
            android:id="@+id/sendMessageButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:background="@drawable/send_button"
            android:text="d" />

    </LinearLayout>


</RelativeLayout>

所以有人可以帮我左右发消息吗? 提前致谢。

【问题讨论】:

  • 试过设置gravity="Start" & "End"??
  • @PrateekRathore 你能给我看一个开始和结束的例子吗?这样我就可以在代码中实现了。
  • 在 xml 文件中试一试,如果你得到它的工作,动态放置它
  • 使你的父布局宽度匹配_parent
  • 根据 getViewType 决定显示左右布局的两个布局,一个用于右侧,另一个用于左侧

标签: android listview alignment


【解决方案1】:

在您的 Layout 文件夹中,您应该创建 2 个不同的 xml 文件:rightchatbubble.xml 和 leftchatbubble.xml 机器人:layout_gravity="对" 和 机器人:layout_gravity="左" 分别。

在您的适配器中,您应该更改以下内容:

 if(hashMap.get("is_mine").equals("yes")) {
                layout.setBackgroundResource(R.drawable.bg_msg1);
                parent_layout.setGravity(Gravity.RIGHT);
            } else {
                layout.setBackgroundResource(R.drawable.bg_msg2);
                parent_layout.setGravity(Gravity.LEFT);
            }

与:

if (hashMap.get("is_mine").equals("yes")) {
                    convertView = LayoutInflater.from(getContext()).inflate(R.layout.leftchatbubble, parent, false);
                }
                else {
                    convertView = LayoutInflater.from(getContext()).inflate(R.layout.rightchatbubble, parent, false);
                }

它应该可以正常工作。 显然,主要记得更新

YOURadapter.notifyDataSetChanged();

【讨论】:

    【解决方案2】:

    尝试使用动态文本视图,而不是使用列表视图: 创建新的线性布局,如

        Lineqarlayout ll =new Linear Layout();
         LayoutParams lparams = new LayoutParams(
        LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
          TextView tvIncoming=new TextView(this);
         tvIncoming.setLayoutParams(lparams);
         tvIncoming.setGravity(GRAVITY.RIGHT);
         tvIncoming.setText("test");
        this.ll.addView(tv);
      //similarly tvOutgoing with gravity left
    
        if(hashMap.get("is_mine").equals("yes")) {
            tvOutgoing.setBackgroundResource(R.drawable.bg_msg1);
           holder.txtMsg.setText(hashMap.get("message"));
          } else {
            tvincogoing.setBackgroundResource(R.drawable.bg_msg1);
           holder.txtMsg.setText(hashMap.get("message"));
          }
    

    如果有帮助请告诉我::) 编辑:在您的活动中使用此代码(或伪代码),而不是在适配器中

    【讨论】:

    • 不,它不工作。文本总是显示在左侧。不知道为什么?
    • @Ankit 你是用同一个列表视图还是做动态布局?
    • 我正在使用相同的列表视图。
    • 我会说不要使用列表视图,因为列表视图呈现它的视图,而文本视图是记住列表视图不是视图组的一部分。所以从 hashmap 获取您的消息并将其添加到动态文本视图中每次..应该我使用您的哈希图相应地编辑我的代码
    • 我添加了 activity_user_chat.xml 文件。请再次查看我更新的代码。
    【解决方案3】:

    您应该将layout_gravity 设置为向左或向右,而不是gravity

    我从How to set layout_gravity programmatically?复制概念

    示例(警告,代码未经测试):

        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
    
    
        if(hashMap.get("is_mine").equals("yes")) {
            layout.setBackgroundResource(R.drawable.bg_msg1);
            params.gravity = Gravity.RIGHT;
            parent_layout.setParams(params);
        } else {
            layout.setBackgroundResource(R.drawable.bg_msg2);
            params.gravity = Gravity.LEFT;
            parent_layout.setParams(params);
            parent_layout.setGravity(Gravity.LEFT);
        }
    

    或者,您可以创建 2 个不同的 XML,并在 xml 内部分配 layout_gravity,并为每一行添加适当的布局。

    【讨论】:

    • @Ankit 尝试将 bubble_layout_parent 上的 layout_width 更改为 match_parent 并设置 bubble_layoutlayout_gravity 而不是 bubble_layout_parent
    • 我试过了,但还是不行。我是否需要发布 main listview.xml 以加深理解?
    • 您的列表视图 layout_width=match_parent 吗?如果是,我认为没有问题
    • 我添加了 activity_user_chat.xml 文件。请再次查看我更新的代码。
    • 请尝试将 xml 中的 listview layout_width 更改为 match_parent,我认为这就是问题所在
    【解决方案4】:

    如果 is_mine 为 yes,则为 is_mine 使用左右两个不同的 textview 为另一个设置可见性 GONE 并在该 textview 上设置您的文本,反之亦然。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-07-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-16
      • 1970-01-01
      • 2014-09-26
      相关资源
      最近更新 更多