java Android:setBackground方法更改视图高度
当我以编程方式更改背景色时,它会拉伸TextView
的宽度
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
holder.textCategory.setText(categoryList.get(position).getName());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
if (categoryIndex == position) {
holder.textCategory.setBackground(context.getDrawable(R.drawable.categories_background));
} else {
holder.textCategory.setBackground(context.getDrawable(R.drawable.temp_background));
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<安卓x.cardview.widget.CardView
安卓:layout_margin="3dp"
安卓:background="@drawable/categories_background"
xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
安卓:orientation="horizontal"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content">
<TextView
安卓:textStyle="bold"
安卓:fontFamily="sans-serif-light"
安卓:textAllCaps="true"
安卓:id="@+id/categoryName"
安卓:paddingBottom="10dp"
安卓:paddingTop="10dp"
安卓:paddingRight="10dp"
安卓:paddingLeft="10dp"
安卓:textColor="#262626"
安卓:textSize="16sp"
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content"/>
</安卓x.cardview.widget.CardView>
每当我点击时,TextView
的宽度就会变宽。但是,我想保持宽度不变,只改变背景颜色
# 1 楼答案
里面有没有空格或不同的字符?你能调试一下吗?谢谢