在Android Studio 3.5中 Android 6.0
这是我的风格:
<style name="buttonEnableStyle" parent="Widget.MaterialComponents.Button.UnelevatedButton">
<item name="android:textColor">@color/text_color_states_materialbuttons</item>
<item name="backgroundTint">@color/color_states_materialbutton</item>
<item name="android:textAppearance">@style/byttonTexAppearanceStyle</item>
</style>
<style name="buttonClickStyle" parent="@style/Widget.MaterialComponents.Button">
<item name="android:textColor">#ffc400</item>
<item name="backgroundTint">#3F3F3F</item>
</style>
xml布局:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<import type="android.view.View" />
</data>
<com.google.android.material.button.MaterialButton
android:id="@+id/buttonStartSearchBluetooth"
style="@style/buttonEnableStyle"
android:layout_width="0dp"
android:layout_height="@dimen/button_height"
android:layout_margin="@dimen/button_margin"
android:onClick="onClickButtonStartSearch"
android:text="@string/start_find"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
我尝试通过编程方式在MaterialButton上更改样式,
TextViewCompat.setTextAppearance(
dataBinding.buttonStartSearchBluetooth,
R.style.buttonClickStyle
)
但是结果是它只能更改文本颜色。但是背景颜色不变
答案 0 :(得分:1)
方法 public void addButtonClicked(View view){
String naStr = etName.getText().toString();
String phStr = etPhone.getText().toString();
if(naStr.isEmpty() || phStr.isEmpty()){
Toast.makeText(getApplicationContext(),"Please, fill-in missing data", Toast.LENGTH_LONG).show();
return;
}
db.execSQL("insert into "+ dbHandler.TABLE_NAME + "(" + dbHandler.COLUMN_NAME + ","+ dbHandler.COLUMN_PHONE +") VALUES (?,?)", new String[] {naStr,phStr});
String tstMsg;
tstMsg = "Name: " + naStr + ", Phone: " + phStr + " is inserted";
Toast.makeText(getApplicationContext(),tstMsg,Toast.LENGTH_LONG).show();
etName.setText("");
etPhone.setText("");
}
I want to delete specific by id
public void delTsk(View view){
String id = etID.getText().toString();
if(id.isEmpty()){
Toast.makeText(getApplicationContext(),"Please, fill-in missing data",Toast.LENGTH_LONG).show();
return;
}
仅更改{strong>文本外观,而不更改背景或setTextAppearance
的样式。
要更改Button
中的颜色和textAppearance,可以使用类似以下内容的方法:
Button
只是关于//For background color
button.setBackgroundTintList(ContextCompat.getColorStateList(this,R.color...));
// or button.setBackgroundColor(ContextCompat.getColor(this,R.color....))
// For TextAppearance
button.setTextAppearance(..);
的注释。
它定义了以下属性:
TextAppearance
检查this post以获得更多详细信息。