【问题标题】:Set up TextView's background to fill only my text - Java设置 TextView 的背景以仅填充我的文本 - Java
【发布时间】:2018-04-07 18:26:02
【问题描述】:

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

我的布局中有一些文本视图。其中一些将 layout_width 设置为 match_parent 并且将重力设置为正确。 我的问题是: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;
    }
}

提前感谢您的帮助。

【问题讨论】:

  • 如你所说,你将 match_parent 赋予你的 textview
  • 我设置了 match_parent 因为否则 setGravity(right) 没有影响
  • 那么图像是你想要的还是你不想要的?
  • 这是我不想要的图像
  • 那么你不只是想要“wrap_content”而不是“match_parent”吗?

标签: android android-layout


【解决方案1】:

只需使用"wrap_content" 而不是"match_parent" 作为您在这些文本视图上的宽度。

:)

【讨论】:

  • 好的,谢谢你的笨蛋;)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-01-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多