android - 如何阻止背景颜色代码覆盖 xml 中的圆角代码?
问题描述
我在 layout.xml 中编码了以下按钮
<Button
android:id="@+id/buttonCorrection"
android:layout_width="0dp"
android:layout_height="30dp"
android:layout_marginTop="4dp"
android:gravity="center_vertical|center_horizontal"
android:text="Undo last entry"
android:textSize="@dimen/button_text_size"
android:textAllCaps="false"
android:textColor="@color/colorAgainstBackground"
android:background="@drawable/clickcorrectionbutton"
app:layout_constraintEnd_toEndOf="@+id/textviewPlayerB"
app:layout_constraintStart_toStartOf="@+id/textviewPlayerA"
app:layout_constraintTop_toBottomOf="@+id/textviewPlayerA" />
clickcorrectionbuttion.xml 用于设置按钮的背景颜色和圆角:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle" >
<corners android:radius="10dp"></corners>
<solid android:color="@color/colorCorrection"></solid>
</shape>
</item>
</selector>
这完美地工作。但是,在应用程序中的某些时候,Java 代码会要求按钮更改颜色和文本。该代码如下所示...
buttonCorrection.setBackgroundColor(getColor(R.color.colorAgainstBackground)); buttonCorrection.setTextColor(getColor(R.color.colorAgainstButton)); buttonCorrection.setText("选择按钮");
问题是,当调用该 java 代码时,它确实会根据需要更改按钮的背景颜色和文本,但它也会删除圆角。它似乎以某种方式覆盖了选择器中的内容。怎样才能让边角保持圆润?
解决方案
我为每个按钮状态制作了一个选择器 xml。背景选择 java 代码现在看起来像
buttonCorrection.setBackground(getDrawable(R.drawable.clickcorrectionbutton))
和
buttonCorrection.setBackground(getDrawable(R.drawable.clickcorrectionbuttonnonactive))
推荐阅读
- javafx-2 - 禁用 JavaFx 组合框
- amazon-web-services - 适用于 SSM 获取参数的 AWS SAM 托管策略
- kotlin - 改造 2. 如果代码为 200,如何处理响应中的错误 JSON
- javascript - 转换 javascript 对象列表
- c - 仅知道其指针即可查找字符数组的大小
- ios - SHA256 Swift 到 Objective C 的等效性
- sas - proc stdize 产生错误的百分位数值
- c++ - 为什么在这种情况下内联函数会花费更多时间?代码如下:
- openstreetmap - 无法连接到立交桥
- javascript - 在 node.js 中使用 Async/await 的正确方法