如何解决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"?>
<androidx.cardview.widget.CardView
android:layout_margin="3dp"
android:background="@drawable/categories_background"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:textStyle="bold"
android:fontFamily="sans-serif-light"
android:textAllCaps="true"
android:id="@+id/categoryName"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:paddingRight="10dp"
android:paddingLeft="10dp"
android:textColor="#262626"
android:textSize="16sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</androidx.cardview.widget.CardView>
每点击一次TextView
的宽度就会变宽。但是,我想保持宽度不变,只改变背景颜色。
解决方法
其中是否有空格或其他字符?您可以通过调试检查它吗?谢谢
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。