簡體   English   中英

如何在 Android 中以編程方式使列表視圖項左右對齊

[英]How to make list view items align left and right programmatically in Android

我正在開發一個聊天應用程序,在聊天視圖中,我必須左右顯示消息。 但是由於我對 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>

和 ChatApapter.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>

那么有人可以幫我左右發送消息嗎? 提前致謝。

你應該設置你的layout_gravity向左或向右,而不是gravity

我正在從如何以編程方式設置 layout_gravity 中復制概念

示例(警告,代碼未測試):

    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 ,並為每一行增加適當的布局。

而不是使用 listview 嘗試使用動態 textview :創建新的線性布局,如

    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"));
      }

如果有幫助,請告訴我 ::) 編輯:在您的活動中而不是在適配器中使用此代碼(或偽代碼)

如果 is_mine 是,則為 is_mine 使用左右兩個不同的 textview 為另一個設置可見性 GONE 並在該 textview 上設置您的文本,反之亦然。

在您的 Layout 文件夾中,您應該創建 2 個不同的 xml 文件:rightchatbubble.xml 和 leftchatbubble.xml,分別使用 android:layout_gravity="right" 和 android:layout_gravity="left"。

在您的適配器中,您應該更改以下內容:

 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();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM