设置TextView的背景以仅填充我的文本 - Java

时间:2018-04-07 10:04:47

标签: android android-layout

我对聊天视图布局有疑问。

我的布局中有一些文字视图。其中一些将layout_width等于match_parent,将gravity设置为right。 我的问题是:textview的背景填充textview的所有可用空间,而不仅仅是textview中的文本。 如何设置textview的背景以仅填写我的文字?

这是我的列表适配器:

    private class CommentAdapter extends ArrayAdapter<COMMENT> {

    RelativeLayout listLayout;
    RelativeLayout.LayoutParams paramsTxt;
    RelativeLayout.LayoutParams paramsImg;

    public CommentAdapter(Context context, ArrayList<COMMENT> comments) {
        super(context, -1, -1, comments);
        listLayout = new RelativeLayout(CommentPage.this);

        paramsTxt = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

        paramsImg = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        paramsImg.height = width_square*7;
        paramsImg.width = width_square*7;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        final ViewHolder holder;
        COMMENT single_comment = null;
        RequestOptions options = new RequestOptions();
        options.centerCrop();

        if (convertView == null) {
            // if convertView is null
            convertView = getLayoutInflater().inflate(R.layout.row_comment, parent, false);
            holder = new ViewHolder();
            // initialize views
            holder.textView = convertView.findViewById(R.id.comment_text);
            holder.imageView = convertView.findViewById(R.id.comment_image);
            convertView.setTag(holder);
        } else {
            holder = (CommentPage.ViewHolder) convertView.getTag();
            holder.imageView.setBackground(null);
        }

        single_comment = getItem(position);

        if(single_comment.senderId.equals(myId)) {
            holder.textView.setGravity(Gravity.RIGHT);
            paramsTxt.setMargins(width_square*20,width_square*2,
                    width_square*5,width_square*2);
            holder.textView.setBackgroundResource(R.drawable.bubble_right);
            holder.textView.setLayoutParams(paramsTxt);
        } else {
            paramsTxt.setMargins(width_square*5,width_square*2,
                    width_square*20,width_square*2);
            paramsImg.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
            paramsTxt.addRule(RelativeLayout.RIGHT_OF, R.id.comment_image);
            holder.imageView.setImageResource(R.drawable.default_profile);
            holder.textView.setBackgroundResource(R.drawable.bubble_left);
            paramsImg.setMargins(width_square*5,width_square*2,
                    0,width_square*2);
            holder.imageView.setLayoutParams(paramsImg);
            holder.textView.setLayoutParams(paramsTxt);
        }

        holder.textView.setText(single_comment.text);

        return convertView;
    }
}

chatview picture

提前感谢您的帮助。

1 个答案:

答案 0 :(得分:2)

在这些文字视图中,只需使用"wrap_content"代替"match_parent"作为宽度。

:)