在这个 android 项目中,我创建了一个默认的按钮样式。我的styles.xml
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:buttonStyle">@style/ButtonStlye123</item>
</style>
<style name="ButtonStlye123" parent="android:style/Widget.Button">
<item name="android:textSize">19dp</item>
<item name="android:layout_margin">8dp</item>
<item name="android:padding">8dp</item>
<item name="android:textColor">@color/ColorDivider</item>
<item name="android:background">@color/ColorAccent</item>
</style>
Run Code Online (Sandbox Code Playgroud)
我的按钮在一个片段中。
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btnLogin"
android:id="@+id/btn_login"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:theme="@style/ButtonStlye123"
android:onClick="login" />
Run Code Online (Sandbox Code Playgroud)
在 android studio 的预览中,它看起来和我想要的完全一样,但是当我在我的设备上运行应用程序时,背景颜色是灰色(“默认按钮颜色”)。如果我更改 style.xml 中的文本颜色,例如:
<item name="android:textColor">@color/ColorPrimary</item>
Run Code Online (Sandbox Code Playgroud)
它改变了(也在我的设备上)所以自定义样式正在加载,我为按钮尝试了不同的颜色(适用于文本)但它不会改变。
为什么我的背景颜色没有加载?
Hez*_*Ziv 100
尝试使用 AppCompactButton 而不是
<Button
Run Code Online (Sandbox Code Playgroud)
用
<androidx.appcompat.widget.AppCompatButton
Run Code Online (Sandbox Code Playgroud)
这将解决问题
更新:01/06/2021
发现上述内容不适用于早期的 Android 版本。用于材料设计按钮使用
app:backgroundTint="@color/green"
Run Code Online (Sandbox Code Playgroud)
小智 12
<style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">
Run Code Online (Sandbox Code Playgroud)
用。。。来代替
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
Run Code Online (Sandbox Code Playgroud)
将 MaterialComponents 更改为 AppCompat 对我有用..
Boo*_*ger 10
我认为您需要将您的属性从“主题”更改为“样式”。这些文件用于不同的事情,因此使用方式不同。
Style 用于为单个视图或组件分配属性,Theme 用于将属性应用于整个应用程序。
所以试试这个:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btnLogin"
android:id="@+id/btn_login"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
<!-- This attribute was changed -->
android:style="@style/ButtonStlye123"
android:onClick="login" />
Run Code Online (Sandbox Code Playgroud)
您还应该根据需要将这些不同类型的 XML 拆分为正确的文件(styles.xml 和 themes.xml - 而不是将它们全部保存在同一个文件中)。这是约定俗成的,不会影响代码编译或执行,但推荐作为代码风格。
使用这个属性backgroundTint会对你有所帮助。它对我有用。例子 :
android:backgroundTint="@color/md_orange_800"
Run Code Online (Sandbox Code Playgroud)
小智 6
如果您使用的是 Android Studio 4.1 及更高版本(测试版),请检查您的 theme.xml 文件中的值。
<style name="Theme.NavaFlashLightV4" parent="Theme.MaterialComponents.DayNight.NoActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">@color/purple_500</item>
<item name="colorPrimaryVariant">@color/purple_700</item>
<item name="colorOnPrimary">@color/white</item>
</style>
Run Code Online (Sandbox Code Playgroud)
将属性更改为@null
<style name="Theme.NavaFlashLightV4" parent="Theme.MaterialComponents.DayNight.NoActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">@null</item> <!-- HERE -->
<item name="colorPrimaryVariant">@color/purple_700</item>
<item name="colorOnPrimary">@color/white</item>
</style>
Run Code Online (Sandbox Code Playgroud)
对于其他旧版本的 Android Studio
只需更改此属性
<item name="colorPrimary">@color/colorPrimary</item>
Run Code Online (Sandbox Code Playgroud)
到
<item name="colorPrimary">@color/@null</item>
Run Code Online (Sandbox Code Playgroud)
最后将按钮的背景设置为您想要的可绘制图标。例子:
<Button
android:id="@+id/btnSwitch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="@drawable/ic_launcher_background" /> <!--the drawable you want-->
Run Code Online (Sandbox Code Playgroud)
小智 6
请使用androidx.appcompat.widget.AppCompatButton insteas的按钮。
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/button_id_Login"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_below="@id/textInnputLayout_editText_id_password"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"
android:text="@string/Login_screen_button_text"
android:background="@drawable/login_button_style"
android:textAllCaps="false">
</androidx.appcompat.widget.AppCompatButton>
Run Code Online (Sandbox Code Playgroud)
您可能正在使用targetSdkVersion30
解决方案:将theme.xml 样式从
<style name="AppTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar">
Run Code Online (Sandbox Code Playgroud)
到
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
Run Code Online (Sandbox Code Playgroud)
你只需要从改变你的应用程序的主题Theme.MaterialComponents.DayNight.DarkActionBar,以Theme.AppCompat.Light.NoActionBar内部styles.xml文件
例子 :
从 : style name="AppTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar"
到 : style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"
| 归档时间: |
|
| 查看次数: |
59858 次 |
| 最近记录: |