在这个 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)
它改变了(也在我的设备上)所以自定义样式正在加载,我为按钮尝试了不同的颜色(适用于文本)但它不会改变。
为什么我的背景颜色没有加载?
我的应用程序中有以下样式.
<style name="BtnPressed">
<item name="android:background">@drawable/btn_default</item>
</style>
Run Code Online (Sandbox Code Playgroud)
现在我想将它应用于ImageButtons我的应用程序中的所有内容.我试图使用这个问题中提到的解决方案,但它没有用.我正在使用API-7.
这
不起作用,因为没有buttonStyle项目名称.它只有button.
我的例子:
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="AppTheme" parent="@android:style/Theme.Black">
<item name="android:button">@style/ThemeButton</item>
</style>
<style name="ThemeButton" parent="@android:style/Widget.Button">
<item name="android:background">@drawable/button</item>
</style>
</resources>
Run Code Online (Sandbox Code Playgroud)
但这也不起作用.