Android 如何防止背景色代码覆盖xml中的圆角代码?
我在layout.xml中编码了以下按钮Android 如何防止背景色代码覆盖xml中的圆角代码?,android,android-layout,android-studio-3.0,Android,Android Layout,Android Studio 3.0,我在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 l
<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.coloraginsbutton));
buttonCorrection.setText(“选择按钮”)
问题是,当调用java代码时,它确实会根据需要更改按钮的背景颜色和文本,但它也会删除圆角。它似乎以某种方式覆盖了选择器中的内容如何保持圆角?我为每个按钮状态创建了一个选择器xml。背景选择java代码现在看起来像 按钮校正.设置背景(getDrawable(R.drawable.ClickCorrection按钮)) 及
buttonCorrection.setBackground(getDrawable(R.drawable.ClickCorrection ButtonOnActive))使用
buttonCorrection.getDrawable().setColorFilter(此处为颜色,PorterDuff.Mode.SRC_顶部为颜色)
而不是setBackgroundColor
@akaki不幸的是getDrawable()“方法无法解决”有人能告诉我为什么这个问题“没有显示任何研究成果。它不清楚或没有用处”?对不起<代码>getBackground()