如何从屏幕边缘推开聊天气泡?

时间:2017-06-27 20:55:00

标签: java android xml

我正在建立聊天活动。我想从边缘推开black chatbubble。下面是我使用的代码(大多数代码不是我的代码)。我尝试了所有我知道的东西,没有任何效果。

Chats.java

package com.xx.xxxxxxx;

import java.util.ArrayList;
import java.util.Random;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;

import com.xx.xxxxxxx.ChatAdapter;
import com.xx.xxxxxxx.ChatMessage;
import com.xx.xxxxxxx.CommonMethods;
import com.xx.xxxxxxx.R;

public class Chats extends Fragment implements OnClickListener {

    private EditText msg_edittext;
    private String user1 = "khushi", user2 = "khushi1";
    private Random random;
    public static ArrayList<ChatMessage> chatlist;
    public static ChatAdapter chatAdapter;
    ListView msgListView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_welcome, container, false);
        random = new Random();
        ((AppCompatActivity) getActivity()).getSupportActionBar().setTitle(
                "Chats");
        msg_edittext = (EditText) view.findViewById(R.id.editText);
        msgListView = (ListView) view.findViewById(R.id.messegesListView);
        Button sendButton = (Button) view
                .findViewById(R.id.button10);
        sendButton.setOnClickListener(this);

        // ----Set autoscroll of listview when a new message arrives----//
        msgListView.setTranscriptMode(ListView.TRANSCRIPT_MODE_ALWAYS_SCROLL);
        msgListView.setStackFromBottom(true);

        chatlist = new ArrayList<ChatMessage>();
        chatAdapter = new ChatAdapter(getActivity(), chatlist);
        msgListView.setAdapter(chatAdapter);
        return view;
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
    }

    public void sendTextMessage(View v) {
        String message = msg_edittext.getEditableText().toString();
        if (!message.equalsIgnoreCase("")) {
            final ChatMessage chatMessage = new ChatMessage(user1, user2,
                    message, "" + random.nextInt(1000), true);
            chatMessage.setMsgID();
            chatMessage.body = message;
            chatMessage.Date = CommonMethods.getCurrentDate();
            chatMessage.Time = CommonMethods.getCurrentTime();
            msg_edittext.setText("");
            chatAdapter.add(chatMessage);
            chatAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button10:
                sendTextMessage(v);

        }
    }

}

ChatActivity.java

package com.xx.xxxxxxx;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Window;

public class ChatActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getSupportActionBar().hide();
        setContentView(R.layout.activity_chat);

    }
}

ChatAdapter.java

package com.xx.xxxxxxx;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
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.LinearLayout;
import android.widget.TextView;
import java.util.ArrayList;

import static android.R.id.list;

public class ChatAdapter extends BaseAdapter {

    private static LayoutInflater inflater = null;
    ArrayList<ChatMessage> chatMessageList;

    public ChatAdapter(Activity activity, ArrayList<ChatMessage> list) {
        chatMessageList = list;
        inflater = (LayoutInflater) activity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    }

    @Override
    public int getCount() {
        return chatMessageList.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ChatMessage message = (ChatMessage) chatMessageList.get(position);
        View vi = convertView;
        if (convertView == null)
            vi = inflater.inflate(R.layout.chatbubble, null);

        TextView msg = (TextView) vi.findViewById(R.id.message_text);
        msg.setText(message.body);
        msg.setTextSize(16);
        LinearLayout layout = (LinearLayout) vi
                .findViewById(R.id.bubble_layout);
        LinearLayout parent_layout = (LinearLayout) vi
                .findViewById(R.id.bubble_layout_parent);
        layout.setPadding(15,11,15,15);

        // if message is mine then align to right
        if (message.isMine) {
            layout.setBackgroundResource(R.drawable.chat_bubble_right);
            parent_layout.setGravity(Gravity.RIGHT);
        }
        // If not mine then align to left
        else {
            layout.setBackgroundResource(R.drawable.chat_bubble_left);
            parent_layout.setGravity(Gravity.LEFT);
        }
        if (message.isMine) {
            msg.setTextColor(Color.WHITE);
        }

        else {
            msg.setTextColor(Color.BLACK);
        }

        return vi;
    }

    public void add(ChatMessage object) {
        chatMessageList.add(object);
    }
}

chat_bubble_left.xml和chat_bubble_right.xml

&#13;
&#13;
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="#ffffff"/>
    <corners
        android:topRightRadius="15dp"
        android:topLeftRadius="15dp"
        android:bottomRightRadius="15dp"
        android:bottomLeftRadius="15dp"/>

</shape>
&#13;
&#13;
&#13;

&#13;
&#13;
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="#000000"/>
    <corners
        android:bottomLeftRadius="20dp"
        android:bottomRightRadius="20dp"
        android:topLeftRadius="20dp"
        android:topRightRadius="20dp"/>

</shape>
&#13;
&#13;
&#13;

chatbubble.xml

&#13;
&#13;
<?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/chat_bubble_right">

        <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>
&#13;
&#13;
&#13;

activity_chat.xml

&#13;
&#13;
<?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:id="@+id/activity_chat"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.bk.cryptit.ChatActivity">
    <fragment
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:name="com.bk.cryptit.Chats"
        tools:layout="@layout/activity_welcome"></fragment>

</RelativeLayout>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

你可以为你的聊天气泡布局添加保证金,

<?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:layout_marginRight="16dp"
        android:layout_marginLeft="16dp"
        android:background="@drawable/chat_bubble_right">

        <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>
相关问题