Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/186.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Android中以编程方式使列表视图项左右对齐_Android_Listview_Alignment - Fatal编程技术网

如何在Android中以编程方式使列表视图项左右对齐

如何在Android中以编程方式使列表视图项左右对齐,android,listview,alignment,Android,Listview,Alignment,我正在使用聊天应用程序,在聊天视图中,我必须左右显示消息。但由于我对Android编程相当陌生,我无法实现这一点。以下是我得到的: 这是我的chatbuble.xml,用于显示行项目 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="

我正在使用聊天应用程序,在聊天视图中,我必须左右显示消息。但由于我对Android编程相当陌生,我无法实现这一点。以下是我得到的:

这是我的chatbuble.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;
        }
@覆盖
公共视图getView(int位置、视图转换视图、视图组父视图){
LayoutInflater LayoutInflater=(LayoutInflater)context.getSystemService(context.LAYOUT\u INFLATER\u SERVICE);
视图=layoutInflater.flate(资源、父项、false);
支架=新支架();
holder.txtMsg=(TextView)view.findViewById(R.id.message\u text);
HashMap HashMap=新的HashMap();
hashMap=chats.get(位置);
LinearLayout布局=(LinearLayout)view.findViewById(R.id.bubble\u布局);
LinearLayout父项\u布局=(LinearLayout)视图.findViewById(R.id.bubble\u布局\u父项);
布局。设置填充(20,20,20,20);
LinearLayout.LayoutParams params=新的LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_内容,LinearLayout.LayoutParams.WRAP_内容);
参数设置边距(0,0,0,20);
布局。setLayoutParams(参数);
if(hashMap.get(“是我的”)等于(“是”){
布局。设置背景资源(R.drawable.bg\U msg1);
父布局。设置重力(重力。右);
}否则{
布局。设置背景资源(R.drawable.bg\U msg2);
父布局。设置重力(重力。左);
}
holder.txtmg.setText(hashMap.get(“message”));
holder.txtmg.setTextColor(Color.BLACK);
返回视图;
}
下面是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>

有人能帮我左右传递信息吗。
提前感谢。

请尝试使用动态文本视图,而不要使用listview: 创建新的线性布局,如

    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"));
      }
如果有帮助,请告诉我:)
编辑:在活动中使用此代码(或伪代码),而不是在适配器中使用此代码。

如果“是我的”,则为“是我的”使用两个不同的文本视图左视图和右视图。如果“是我的”,则为另一个设置可见性,并在该文本视图上设置文本,反之亦然。

您应该将
布局设置为左视图或右视图,而不是重力

我是在复制这个概念

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


或者,创建两个不同的xml,并在xml内部分配
layout\u gravity
,并为每一行充气适当的布局。

在布局文件夹中,您应该创建两个不同的xml文件:rightchatbubble.xml和leftchatbubble.xml android:layout\u gravity=“右” 和 android:layout\u 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);
            }
与:

它应该很好用。 显然,总的来说要记得更新

YOURadapter.notifyDataSetChanged();

试过设置gravity=“Start”和“End”?@prateekratore你能给我举个开始和结束的例子吗。这样我就可以在代码中实现了。在xml文件中尝试一下,如果你能让它工作的话,就动态地把它放在你的父布局宽度上,使之与父布局宽度相匹配。根据getViewType设置两个布局,一个是右布局,另一个是左布局。决定显示哪个布局是右布局和左布局不,它不工作。。文本始终显示在左侧。我不知道为什么?@Ankit您使用的是相同的listview还是动态布局。?我使用的是相同的列表视图。我会说不要使用listview,因为列表视图呈现其视图,而textview是其中的一部分。请记住listview不是视图组。所以请从hashmap获取您的消息,每次都将其添加到动态textview中。。我是否应该使用hashmapI相应地编辑代码?我已经添加了activity\u user\u chat.xml文件。请再次查看我更新的代码。@Ankit尝试将
bubble\u layout\u parent
上的布局宽度更改为
match\u parent
,并设置
bubble\u layout
layout\u重力
,而不是
bubble\u layout\u parent
。我尝试了此操作,但仍然无效。我需要发布main listview.xml以加深理解吗?您的listview布局\u width=match\u父级吗?如果是,我认为没有问题。我已经添加了activity\u user\u chat.xml文件。请再次查看我的更新代码。请尝试更改xml中的listview布局宽度以匹配父项,我认为这就是问题所在
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();