【发布时间】:2018-04-28 06:21:35
【问题描述】:
Here is what the button looks like after changing the color
Here is what it looks like with "show layout bounds" on
我有一个“添加注释”按钮,它显示一个对话框供用户输入注释。
如果保存了笔记,我想更改按钮的颜色。
我试过了:
btnNote.setBackgroundColor(view.getContext().getResources().getColor(R.color.NN));
还有这个:
btnNote.getBackground().setColorFilter(ContextCompat.getColor(view.getContext(), R.color.NN), PorterDuff.Mode.MULTIPLY);
但在这两种情况下,按钮也会变得稍大。
如何只更改按钮的颜色?
这是我的布局文件中的按钮:
<Button
android:id="@+id/btnNote"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:text="Add note"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/btnDelete"
app:layout_constraintTop_toTopOf="parent" />
【问题讨论】:
-
在其中添加一些填充,然后在更改颜色期间大小不会调整大小
-
我添加了android:padding="2dp",但这并没有改变结果
-
改变高度一旦达到 50 dp
-
我试过了,结果是一样的
-
然后使用一次这个 btnNote.setBackgroundResource(Color.parse(colorName));