如何使用 com.google.android.material 对象和更改按钮背景
How to use com.google.android.material Objects and change button background
我有问题我想同时更改按钮颜色和使用 com.google.android.material
对象
我发现当我更改主题中的行时:<style name="Theme.TeoriaLearning" parent="Theme.MaterialComponents.Light.DarkActionBar">
到这一行:<style name="Theme.TeoriaLearning" parent="Theme.AppCompat.Light.DarkActionBar">
我可以改变按钮的背景颜色..等等
但是当我这样做时,我也不能使用 com.google.android.material.textfield.TextInputLayout
和 com.google.android.material.textfield.TextInputEditText
或任何其他 com.google.android.material
对象
(我换行后)
当我尝试启动 Activity 应用程序崩溃时,我收到以下错误:
Logcat 错误
2021-04-03 19:22:05.558 29774-29774/com.example.teorialearning E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.teorialearning, PID: 29774
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.teorialearning/com.example.teorialearning.Activies.RegisterActivity}: android.view.InflateException: Binary XML file line #28: Binary XML file line #28: Error inflating class com.google.android.material.textfield.TextInputLayout
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: android.view.InflateException: Binary XML file line #28: Binary XML file line #28: Error inflating class com.google.android.material.textfield.TextInputLayout
Caused by: android.view.InflateException: Binary XML file line #28: Error inflating class com.google.android.material.textfield.TextInputLayout
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance0(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:430)
at android.view.LayoutInflater.createView(LayoutInflater.java:645)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:787)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:858)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:861)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
at android.view.LayoutInflater.inflate(LayoutInflater.java:518)
at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
at com.example.teorialearning.databinding.ActivityRegisterBinding.inflate(ActivityRegisterBinding.java:92)
at com.example.teorialearning.databinding.ActivityRegisterBinding.inflate(ActivityRegisterBinding.java:86)
at com.example.teorialearning.Activies.RegisterActivity.onCreate(RegisterActivity.java:19)
at android.app.Activity.performCreate(Activity.java:6679)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: java.lang.IllegalArgumentException: This component requires that you specify a valid TextAppearance attribute. Update your app theme to inherit from Theme.MaterialComponents (or a descendant).
at com.google.android.material.internal.ThemeEnforcement.checkTextAppearance(ThemeEnforcement.java:187)
at com.google.android.material.internal.ThemeEnforcement.obtainTintedStyledAttributes(ThemeEnforcement.java:118)
at com.google.android.material.textfield.TextInputLayout.<init>(TextInputLayout.java:463)
at com.google.android.material.textfield.TextInputLayout.<init>(TextInputLayout.java:422)
at java.lang.reflect.Constructor.newInstance0(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:430)
at android.view.LayoutInflater.createView(LayoutInflater.java:645)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:787)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:858)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:861)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
at android.view.LayoutInflater.inflate(LayoutInflater.java:518)
at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
at com.example.teorialearning.databinding.ActivityRegisterBinding.inflate(ActivityRegisterBinding.java:92)
at com.example.teorialearning.databinding.ActivityRegisterBinding.inflate(ActivityRegisterBinding.java:86)
at com.example.teorialearning.Activies.RegisterActivity.onCreate(RegisterActivity.java:19)
at android.app.Activity.performCreate(Activity.java:6679)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
那么我该怎么做才能既使用这些对象又能够更改背景颜色?
如果您需要任何代码,请询问,我会编辑并提及您:)
要使用 TextInputEditText,您必须使用 Material 样式。
所以保持你的风格:
<style name="Theme.TeoriaLearning" parent="Theme.MaterialComponents.Light.DarkActionBar">
对于更改按钮背景的第一个问题,请在 xml 布局中使用它:
app:backgroundTint="@color/YourColor"
例如:
<com.google.android.material.button.MaterialButton
style="@style/Widget.MaterialComponents.Button.UnelevatedButton"
app:backgroundTint="@color/purple_700"
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
并以编程方式更改它:
MaterialButton button = findViewById(R.id.button);
button.setBackgroundColor(ContextCompat.getColor(this,R.color.black));
要获得正常效果,请在您的按钮中使用此样式:
style="@style/Widget.MaterialComponents.Button.UnelevatedButton"
编辑
@GabrieleMariotti 建议更好的方法是使用 setBackgroundTintList()
这是一个如何使用它的例子:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MaterialButton button = findViewById(R.id.button);
int[][] states = new int[][]{
new int[]{android.R.attr.state_enabled}, // enabled
new int[]{-android.R.attr.state_enabled}, // disabled
new int[]{android.R.attr.state_pressed} // pressed
};
int[] colors = new int[]{
getColorRes(R.color.purple_700), // enabled color
getColorRes(R.color.purple_200), // disabled color
getColorRes(R.color.purple_500) // pressed color
};
button.setBackgroundTintList(new ColorStateList(states, colors));
}
private int getColorRes(@ColorRes int colorRes) {
return ContextCompat.getColor(this, colorRes);
}
您可以在按钮视图对象上使用 setBackground(Drawable background)
方法通过代码设置背景。 Drawable
对象可以从 res 文件夹中提取,或者您可以根据需要生成具有圆形边框的实心或渐变形状并将其传递给 setBackground()
方法。
它是这样调用的:
buttonObject.setBackground(background);
我有问题我想同时更改按钮颜色和使用 com.google.android.material
对象
我发现当我更改主题中的行时:<style name="Theme.TeoriaLearning" parent="Theme.MaterialComponents.Light.DarkActionBar">
到这一行:<style name="Theme.TeoriaLearning" parent="Theme.AppCompat.Light.DarkActionBar">
我可以改变按钮的背景颜色..等等
但是当我这样做时,我也不能使用 com.google.android.material.textfield.TextInputLayout
和 com.google.android.material.textfield.TextInputEditText
或任何其他 com.google.android.material
对象
(我换行后) 当我尝试启动 Activity 应用程序崩溃时,我收到以下错误:
Logcat 错误
2021-04-03 19:22:05.558 29774-29774/com.example.teorialearning E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.teorialearning, PID: 29774
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.teorialearning/com.example.teorialearning.Activies.RegisterActivity}: android.view.InflateException: Binary XML file line #28: Binary XML file line #28: Error inflating class com.google.android.material.textfield.TextInputLayout
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: android.view.InflateException: Binary XML file line #28: Binary XML file line #28: Error inflating class com.google.android.material.textfield.TextInputLayout
Caused by: android.view.InflateException: Binary XML file line #28: Error inflating class com.google.android.material.textfield.TextInputLayout
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance0(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:430)
at android.view.LayoutInflater.createView(LayoutInflater.java:645)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:787)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:858)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:861)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
at android.view.LayoutInflater.inflate(LayoutInflater.java:518)
at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
at com.example.teorialearning.databinding.ActivityRegisterBinding.inflate(ActivityRegisterBinding.java:92)
at com.example.teorialearning.databinding.ActivityRegisterBinding.inflate(ActivityRegisterBinding.java:86)
at com.example.teorialearning.Activies.RegisterActivity.onCreate(RegisterActivity.java:19)
at android.app.Activity.performCreate(Activity.java:6679)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: java.lang.IllegalArgumentException: This component requires that you specify a valid TextAppearance attribute. Update your app theme to inherit from Theme.MaterialComponents (or a descendant).
at com.google.android.material.internal.ThemeEnforcement.checkTextAppearance(ThemeEnforcement.java:187)
at com.google.android.material.internal.ThemeEnforcement.obtainTintedStyledAttributes(ThemeEnforcement.java:118)
at com.google.android.material.textfield.TextInputLayout.<init>(TextInputLayout.java:463)
at com.google.android.material.textfield.TextInputLayout.<init>(TextInputLayout.java:422)
at java.lang.reflect.Constructor.newInstance0(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:430)
at android.view.LayoutInflater.createView(LayoutInflater.java:645)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:787)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:858)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:861)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
at android.view.LayoutInflater.inflate(LayoutInflater.java:518)
at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
at com.example.teorialearning.databinding.ActivityRegisterBinding.inflate(ActivityRegisterBinding.java:92)
at com.example.teorialearning.databinding.ActivityRegisterBinding.inflate(ActivityRegisterBinding.java:86)
at com.example.teorialearning.Activies.RegisterActivity.onCreate(RegisterActivity.java:19)
at android.app.Activity.performCreate(Activity.java:6679)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
那么我该怎么做才能既使用这些对象又能够更改背景颜色?
如果您需要任何代码,请询问,我会编辑并提及您:)
要使用 TextInputEditText,您必须使用 Material 样式。 所以保持你的风格:
<style name="Theme.TeoriaLearning" parent="Theme.MaterialComponents.Light.DarkActionBar">
对于更改按钮背景的第一个问题,请在 xml 布局中使用它:
app:backgroundTint="@color/YourColor"
例如:
<com.google.android.material.button.MaterialButton
style="@style/Widget.MaterialComponents.Button.UnelevatedButton"
app:backgroundTint="@color/purple_700"
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
并以编程方式更改它:
MaterialButton button = findViewById(R.id.button);
button.setBackgroundColor(ContextCompat.getColor(this,R.color.black));
要获得正常效果,请在您的按钮中使用此样式:
style="@style/Widget.MaterialComponents.Button.UnelevatedButton"
编辑
@GabrieleMariotti 建议更好的方法是使用 setBackgroundTintList()
这是一个如何使用它的例子:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MaterialButton button = findViewById(R.id.button);
int[][] states = new int[][]{
new int[]{android.R.attr.state_enabled}, // enabled
new int[]{-android.R.attr.state_enabled}, // disabled
new int[]{android.R.attr.state_pressed} // pressed
};
int[] colors = new int[]{
getColorRes(R.color.purple_700), // enabled color
getColorRes(R.color.purple_200), // disabled color
getColorRes(R.color.purple_500) // pressed color
};
button.setBackgroundTintList(new ColorStateList(states, colors));
}
private int getColorRes(@ColorRes int colorRes) {
return ContextCompat.getColor(this, colorRes);
}
您可以在按钮视图对象上使用 setBackground(Drawable background)
方法通过代码设置背景。 Drawable
对象可以从 res 文件夹中提取,或者您可以根据需要生成具有圆形边框的实心或渐变形状并将其传递给 setBackground()
方法。
它是这样调用的:
buttonObject.setBackground(background);