简体   繁体   English

使用wrap_content在textview上设置顶部,底部填充

[英]set top,bottom padding on textview with wrap_content

  viewParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            viewParams.setMargins(15, 15, 15, 0);
  TextView text = new TextView(mContext);
  text.setLayoutParams(viewParams);
  text.setGravity(Gravity.CENTER_VERTICAL);
  text.setPadding(50,50,50,50);
  text.setBackground(mContext.getResources().getDrawable(R.drawable.exp_comments));

I want my textview to have top and bottom padding. 我希望我的textview具有顶部和底部填充。 I set background with green-rectangular border around the textview. 我在textview周围设置了带有绿色矩形边框的背景。

I set height with wrap_content. 我用wrap_content设置高度。 and it means that the border is just above the text. 这表示边框刚好在文字上方。 I want to increase the top and bottom gap between the border and the text. 我想增加边框和文本之间的顶部和底部间隙。 So, I tried with setPadding() and setPaddingRelative() method. 因此,我尝试使用setPadding()和setPaddingRelative()方法。 But it doesn't work.... 但这不起作用....

The setPadding method values is in pixels. setPadding方法的值以像素为单位。 You should convert your dp to pixels and then set the padding. 您应该将dp转换为像素,然后设置填充。

int valueInDp = 50;
Resources r = getResources();
int px = (int) TypedValue
    .applyDimension(TypedValue.COMPLEX_UNIT_DIP, valueInDp, r.getDisplayMetrics());
text.setPadding(px,px,px,px);

更改顶部和底部填充,

    text.setPadding(50,100,50,100);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM