在android中动态显示按钮,但是一旦将按钮添加到布局中,它们就会进入每个按钮

时间:2017-04-28 04:02:48

标签: android android-layout android-view chatbot

Screenshot for first message and and how buttons are coming
Screenshot of second message and how buttons shifted down

我正在开发一个聊天Android应用程序 聊天机器人客户端,我想在布局中显示两个按钮 每当有新消息进入时,我的主要活动就会膨胀。如果 它由用户发送,然后右侧,如果由机器人然后左侧。如我一般 在该布局中添加按钮,以便设置条件 按钮然后他们将进入该布局的任何消息 是用户或机器人。当我们输入新消息时,它的按钮就会下降或 他们将收到第二条最新消息。

按钮代码:

ChatActivity.java文件:

if (btnname.contentEquals("Lync")) {
                                    LinearLayout vgjp = (LinearLayout) findViewById(R.id.m123);

                                    Button btn1=(Button) new Button(this);
                                    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                                            ViewGroup.LayoutParams.WRAP_CONTENT);


                                    btn1.setLayoutParams(params);
                                    Button btn2=(Button) new Button(this);
                                    btn2.setLayoutParams(params);


                                    btn1.setText(btnname);
                                    btn2.setText("Other App");

                                    vgjp.addView(btn1);
                                    vgjp.addView(btn2);

                                    btn1.setOnClickListener(new View.OnClickListener() {
                                        public void onClick(View v) {
                                            // Perform action on click
                                            sendMessageToBot("Lync");

                                        }
                                    });
                                    btn2.setOnClickListener(new View.OnClickListener() {
                                        public void onClick(View v) {
                                            // Perform action on click
                                            sendMessageToBot("Other App");
                                        }
                                    });

                                } else if (btnname.contentEquals("Outlook")) {



                                    LinearLayout vgjp = (LinearLayout) findViewById(R.id.m123);

                                    Button btn1=(Button) new Button(context);
                                    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                                            ViewGroup.LayoutParams.WRAP_CONTENT);

                                    btn1.setLayoutParams(params);
                                    Button btn2=(Button) new Button(context);
                                    btn2.setLayoutParams(params);


                                    btn1.setText(btnname);
                                    btn2.setText("Other App");

                                    vgjp.addView(btn1);
                                    vgjp.addView(btn2);
                          btn1.setOnClickListener(new View.OnClickListener() {
                                        public void onClick(View v) {
                                            // Perform action on click
                                            sendMessageToBot("Outlook");
                                        }
                                    });
                          btn2.setOnClickListener(new View.OnClickListener() 
{
                                        public void onClick(View v) {
                                            // Perform action on click
                                            sendMessageToBot("Other App");
                                        }
                                    });


                                }

                            }

此xml文件中的此布局会在从bot或用户收到任何文本时膨胀:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/newid">




    <LinearLayout
        android:id="@+id/content"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:orientation="vertical">



        <TextView
            android:id="@+id/txtInfo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_gravity="center_horizontal"
            android:textSize="12sp"
            android:textColor="@android:color/darker_gray" />


        <LinearLayout
            android:id="@+id/contentWithBackground"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="left"
            android:background="@drawable/in_message_bg"
            android:paddingLeft="10dp"
            android:paddingBottom="10dp"
            android:orientation="vertical">

            <TextView
                android:id="@+id/txtMessage"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="@android:color/black"
                android:maxWidth="240dp" />

            <VideoView
                android:id="@+id/myVideo"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_centerInParent="true" />
     <!-- This code is for Buttons -->
            <ScrollView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">
                <LinearLayout
                    android:orientation="horizontal"
                    android:layout_alignParentBottom="true"
                    android:layout_alignParentLeft="true"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:id="@+id/m123">

                </LinearLayout>
            </ScrollView>

            </LinearLayout>

        </LinearLayout>
    </RelativeLayout>

    This is my main container layout xml file :

    <LinearLayout 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:gravity="center_horizontal"
    android:orientation="vertical"

    android:paddingTop="@dimen/activity_vertical_margin"

 `enter code here`tools:context="com.directlineex.brsingh.directlineandroidexample.ChatActivity">


    <RelativeLayout
        android:id="@+id/container"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="20dp">

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/images"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:id="@+id/mikebutton"

            />


        <EditText
            android:id="@+id/messageEdit"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/mikebutton"
           android:layout_alignParentLeft="false"
            android:layout_alignParentBottom="true"
            android:layout_toLeftOf="@+id/chatSendButton"
            android:hint="Type Query" />

        <Button
            android:id="@+id/chatSendButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:background="@color/background_floating_material_dark"
            android:text="Send MSG"
            android:textColor="@color/background_material_light"/>



        <ListView
            android:id="@+id/messagesContainer"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_marginBottom="20dp"
            android:layout_above="@+id/messageEdit"
            android:layout_below="@+id/meLbl"
            android:layout_marginTop="30dp"
            android:listSelector="@android:color/transparent"
            android:transcriptMode="alwaysScroll"
            android:divider="@null" />

        <TextView
            android:id="@+id/meLbl"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:text="MySelf"
            android:singleLine="false"
            android:textSize="20dp" />

        <TextView
            android:id="@+id/friendLabel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="left|center_vertical"
            android:layout_alignParentTop="true"

            android:text="Friend"
            android:textSize="20dp" />

   </RelativeLayout>

ChatAdapter.java适配器文件

import android.app.Activity;
import android.content.Context;
import android.os.StrictMode;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.directlineex.brsingh.directlineandroidexample.ChatActivity;

import org.json.JSONObject;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;


public class ChatAdapter extends BaseAdapter {
private String localToken = "";
private String conversationId = "";
private String primaryToken = "";
private String botName = "";
private final List<ChatMessage> chatMessages;
private Activity context;




public ChatAdapter(Activity context, List<ChatMessage> chatMessages) {
    this.context = context;
    this.chatMessages = chatMessages;
}

@Override
public int getCount() {
    if (chatMessages != null) {
        return chatMessages.size();
    } else {
        return 0;
    }
}

@Override
public ChatMessage getItem(int position) {
    if (chatMessages != null) {
        return chatMessages.get(position);
    } else {
        return null;
    }
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) 
{
    ViewHolder holder;
    final ChatMessage chatMessage = getItem(position);
    LayoutInflater vi = (LayoutInflater) 
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    if (convertView == null) {
        convertView = vi.inflate(R.layout.list_item_chat_message, null);
        holder = createViewHolder(convertView);
        convertView.setTag(holder);
    } else {
        // holder = createViewHolder(convertView);
        holder = (ViewHolder) convertView.getTag();



    }


    boolean myMsg = chatMessage.getIsme() ;
 setAlignment(holder, myMsg);
holder.txtMessage.setText(chatMessage.getMessage()); //Make a temporary 
variable here for this

    // if(chatMessage.getDate()!=null)
    holder.txtInfo.setText(chatMessage.getDate());

    chatMessage.setB1(null);
    chatMessage.setB2(null);

    return convertView;
}





public void add(ChatMessage message) {

    chatMessages.add(message);

}

public void add(List<ChatMessage> messages) {
    chatMessages.addAll(messages);
}
public void setAlignment(ViewHolder holder,boolean mymsg){

    if (!mymsg) {


holder.contentWithBG.setBackgroundResource(R.drawable.in_message_bg);

        LinearLayout.LayoutParams layoutParams =
                (LinearLayout.LayoutParams) 
holder.contentWithBG.getLayoutParams();
        layoutParams.gravity = Gravity.LEFT;
        holder.contentWithBG.setLayoutParams(layoutParams);
      /*  LinearLayout.LayoutParams layoutParams12 =
                (LinearLayout.LayoutParams) holder.temp.getLayoutParams();
        layoutParams.gravity = Gravity.LEFT;
        holder.temp.setLayoutParams(layoutParams12);*/
        RelativeLayout.LayoutParams lp =
                (RelativeLayout.LayoutParams) 
holder.content.getLayoutParams();
        lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, 0);
        lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
        holder.content.setLayoutParams(lp);

        layoutParams = (LinearLayout.LayoutParams) 
holder.txtMessage.getLayoutParams();
        layoutParams.gravity = Gravity.LEFT;
        holder.txtMessage.setLayoutParams(layoutParams);


        layoutParams = (LinearLayout.LayoutParams) 
holder.txtInfo.getLayoutParams();
        layoutParams.gravity = Gravity.LEFT;
        holder.txtInfo.setLayoutParams(layoutParams);

        //holder.btn1=(Button) new Button(context);
        LinearLayout.LayoutParams params = new 
LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        holder.btn1.setGravity(Gravity.CENTER_HORIZONTAL);
        holder. btn1.setLayoutParams(params);






    } else {



holder.contentWithBG.setBackgroundResource(R.drawable.out_message_bg);

        LinearLayout.LayoutParams layoutParams =
                (LinearLayout.LayoutParams) 
holder.contentWithBG.getLayoutParams();
        layoutParams.gravity = Gravity.RIGHT;

        holder.contentWithBG.setLayoutParams(layoutParams);

        RelativeLayout.LayoutParams lp =
                (RelativeLayout.LayoutParams) 
holder.content.getLayoutParams();
        lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT, 0);
        lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        holder.content.setLayoutParams(lp);
        layoutParams = (LinearLayout.LayoutParams) 
holder.txtMessage.getLayoutParams();
        layoutParams.gravity = Gravity.RIGHT;
        holder.txtMessage.setLayoutParams(layoutParams);

        layoutParams = (LinearLayout.LayoutParams) 
holder.txtInfo.getLayoutParams();
        layoutParams.gravity = Gravity.RIGHT;
        holder.txtInfo.setLayoutParams(layoutParams);

    }
}


private ViewHolder createViewHolder(View v) {
    ViewHolder holder = new ViewHolder();
  /*  holder.btn1=new Button(context);
    holder.btn2=new Button(context);*/
    holder.txtMessage = (TextView) v.findViewById(R.id.txtMessage);

    holder.content = (LinearLayout) v.findViewById(R.id.content);
    // holder.btn1=(Button) v.findViewById(R.id.btn1);
    //holder.btn2=(Button) v.findViewById(R.id.btn2);
    // holder.lyncbtn=(Button) v.findViewById(R.id.lyncbtn);
    //(LinearLayout) v.findViewById(R.id.contentWithBackground);

    holder.contentWithBG = (LinearLayout) 
v.findViewById(R.id.contentWithBackground);
    holder.txtInfo = (TextView) v.findViewById(R.id.txtInfo);
    // holder.temp=(LinearLayout)v.findViewById(R.id.m123);

    holder.btn1=(Button) new Button(context);
    holder.btn2=(Button) new Button(context);
    return holder;
}

private static class ViewHolder {
    public TextView txtMessage;
    public TextView txtInfo;
    public Button btn1;
     public Button btn2;
    //public Button lyncbtn;
    public LinearLayout content;
    public LinearLayout contentWithBG;
    // public LinearLayout temp;

}
}

1 个答案:

答案 0 :(得分:0)

应在回收站视图中添加聊天消息

  

首先在您的聊天活动中查看回收者视图。并为它制作适配器。

     

在适配器传递消息列表中。

     

现在在回收者视图行中有两个布局,一个用于用户,一个用于用户(一个是左对齐,一个是右对齐),两个布局的可见性默认为GONE。

     

当您收到新消息时,请将其添加到消息列表中,并将其设置为消息框布局的左侧。并使其可见并通过notifyDataSetChanged通知适配器。

     

当您发送新消息时,将其添加到消息列表并将其设置为消息框布局的右侧。并使其可见并通过notifyDataSetChanged通知适配器。

它可能对您有所帮助,如果有任何疑问,请评论我。