如何在android中的聊天适配器的listview中设置左右间隙

时间:2015-01-31 08:32:10

标签: android

public class ChatAdapter extends ArrayAdapter<Chat> {
    private final Context context;
    private final ArrayList<Chat> values;
    ImageLoader imageloader;
    Datamodel dm;

    public ChatAdapter(Context context, ArrayList<Chat> values) {
        super(context, R.layout.list_row_layout_even, values);
        // TODO Auto-generated constructor stub
        this.context = context;
        this.values = values;
        // imageloader = new ImageLoader(context);

    }

    public void addMessage(Chat chat) {
        values.add(chat);
        this.notifyDataSetChanged();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub

        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        if (convertView == null)
            convertView = inflater.inflate(R.layout.list_row_layout_odd,
                    parent, false);

        LinearLayout root = (LinearLayout) convertView
                .findViewById(R.id.even_container);
        TextView tv = (TextView) convertView.findViewById(R.id.text);

        CircularImageview userImg = (CircularImageview) convertView
                .findViewById(R.id.user_img);
        CircularImageview oddImg = (CircularImageview) convertView
                .findViewById(R.id.odd_bubble);
        ImageView leftimageicon = (ImageView) root.findViewById(R.id.left);
        ImageView rightimageicon = (ImageView) root.findViewById(R.id.right);

        Typeface fontArial = Typeface.createFromAsset(context.getAssets(),
                "fonts/ARIAL.TTF");

        SharedPreferences prefs = context.getSharedPreferences(
                AppConstants.LOGIN_PREFS, Context.MODE_PRIVATE);
        String K = prefs.getString("Member_id", "");
        Chat chat = values.get(position);

        String t = chat.getRecieverID();

        tv.setText(chat.getMessage());

        tv.setTypeface(fontArial);

        AQuery aq = new AQuery(context);

        if (chat.getSenderID().equals(prefs.getString("Member_id", ""))) {
            root.setBackgroundColor(Color.parseColor("#07000000"));
            tv.setTextColor(Color.parseColor("#636363"));

            rightimageicon.setVisibility(View.GONE);
            leftimageicon.setVisibility(View.VISIBLE);
            String imagePath = Environment.getExternalStorageDirectory()
                    .toString() + "/.LociiImages/" + t + ".jpg";
            Bitmap bitmap = BitmapFactory.decodeFile(imagePath);
            tv.setBackgroundColor(Color.parseColor("#00ad9a"));
            tv.setTextColor(Color.parseColor("#ffffff"));
            oddImg.setImageBitmap(bitmap);
            leftimageicon.setVisibility(View.GONE);
            rightimageicon.setVisibility(View.VISIBLE);
            userImg.setVisibility(View.GONE);
            oddImg.setVisibility(View.GONE);
        }

        else {
            root.setBackgroundColor(Color.parseColor("#07000000"));
            tv.setTextColor(Color.parseColor("#bdbdbd"));
            tv.setBackgroundColor(Color.parseColor("#ffffff"));

            userImg.setVisibility(View.GONE);
            oddImg.setVisibility(View.VISIBLE);
            rightimageicon.setVisibility(View.GONE);
            leftimageicon.setVisibility(View.VISIBLE);
        }

        return convertView;
    }
}

list_row_layout_even.xml

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

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="60dip"
        android:layout_alignParentRight="true"
        android:layout_marginRight="8dip"
        android:layout_marginTop="10dip"
        android:layout_toRightOf="@+id/user_img"
        android:background="#ffffff"
        android:textColor="#636363"
        android:textSize="16dip" />

    <com.lociiapp.utils.CircularImageview
        android:id="@+id/user_img"
        android:layout_width="50dip"
        android:layout_height="50dip"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:src="@drawable/index" />

</RelativeLayout>

list_row_layout_even.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/even_container"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <RelativeLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.20" >

        <com.lociiapp.utils.CircularImageview
            android:id="@+id/odd_bubble"
            android:layout_width="50dip"
            android:layout_height="50dip"
            android:layout_alignParentLeft="true"
            android:layout_margin="5dip"
            android:visibility="visible" />

        <com.lociiapp.utils.CircularImageview
            android:id="@+id/user_img"
            android:layout_width="50dip"
            android:layout_height="50dip"
            android:layout_alignParentRight="true"
            android:layout_margin="5dip"
            android:visibility="visible" />
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/reltaivelayout1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.80" >

        <ImageView
            android:id="@+id/left"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_marginBottom="5dip"
            android:layout_marginTop="15dip"
            android:src="@drawable/callout_left" />

        <TextView
            android:id="@+id/text"
            android:layout_width="225dip"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_marginBottom="5dip"
            android:layout_marginTop="15dip"
            android:layout_toRightOf="@+id/left"
            android:padding="8dp"
            android:textColor="#636363"
            android:textSize="20sp" />

        <ImageView
            android:id="@+id/right"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_marginBottom="5dip"
            android:layout_marginTop="15dip"
            android:layout_toRightOf="@+id/text"
            android:src="@drawable/callout_right" />
    </RelativeLayout>

</LinearLayout>

这是list_row_layout_even.xml我的代码根据条件使用显示textview,如果这样(chat.getSenderID()。equals(prefs.getString(“Member_id”,“”)) )对于文本视图,图像可见消息,如果失败则只有文本视图可见无图像视图。但我很困惑如何在发送和接收文本视图之间的xml文件中运行时设置边距或填充。

我当前的屏幕

enter image description here

欲望屏幕:enter image description here

2 个答案:

答案 0 :(得分:0)

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT);
    if (msgDirection.equals("OUT")) {
                holder.messageLayout.setPadding(10, 5, 18, 25);
                params.setMargins(80, 0, 2, 0);
                params.gravity = Gravity.RIGHT;
                holder.messageLayout.setLayoutParams(params);

            }else {
                holder.messageLayout.setPadding(20, 5, 10, 5);
                params.gravity = Gravity.LEFT;
                params.setMargins(2, 0, 80, 0);
                holder.messageLayout.setLayoutParams(params);
            }

答案 1 :(得分:0)

您可以通过这种方式尝试

在CustomListAdapter.java文件

 if (position % 2 == 0) {
            root.setBackgroundColor(Color.parseColor("#07000000"));
            root.setPadding(60, 0, 0, 0);
            tv.setBackgroundColor(Color.parseColor("#00ad9a"));
            tv.setTextColor(Color.parseColor("#ffffff"));
            leftimageicon.setVisibility(View.GONE);
            rightimageicon.setVisibility(View.VISIBLE);

            oddImg.setVisibility(View.GONE);
        }

        else {
            root.setBackgroundColor(Color.parseColor("#07000000"));
            tv.setTextColor(Color.parseColor("#bdbdbd"));
            root.setPadding(0, 0, 60, 0);
            tv.setBackgroundColor(Color.parseColor("#ffffff"));
            oddImg.setVisibility(View.VISIBLE);

            leftimageicon.setVisibility(View.VISIBLE);
            rightimageicon.setVisibility(View.GONE);
            oddImg.setVisibility(View.VISIBLE);

        }