android - 将按钮样式添加到 AppTheme 不起作用
问题描述
我尝试向我的 AppTheme 添加自定义按钮样式,但它不起作用。作为参考,我在我的 AppTheme 中添加了 Spinner 样式,它运行良好。所以,我只需要找出我的按钮样式哪里出了问题。
样式.xml:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:spinnerStyle">@style/mySpinnerStyle</item>
<item name="android:buttonStyle">@style/myButtonStyle</item>
<item name="buttonStyle">@style/myButtonStyle</item>
</style>
<style name="mySpinnerStyle" parent="@style/Widget.AppCompat.Spinner">
<item name="overlapAnchor">true</item>
<item name="android:background">@drawable/spinner_background</item>
<item name="android:padding">5sp</item>
</style>
<style name="myButtonStyle" parent="@style/Widget.AppCompat.Button">
<item name="android:background">@drawable/button_states</item>
<item name="android:focusable">true</item>
<item name="android:clickable">true</item>
</style>
如果需要,我可以添加我的 button_states.xml 文件,但是如果我将样式直接插入到布局中的按钮属性中,则该样式有效,如下所示:
<Button
android:id="@+id/button_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="4"
style="@style/myButtonStyle"
android:textSize="@dimen/text_size"
android:text="OK" />
父布局中的声明:
android:theme="@style/AppTheme"
那么,为什么它直接在按钮属性中起作用,而不是通过我的 AppTheme 呢?
解决方案
1-通过右键单击您的android studio中的res / drawable文件创建新的DRAWABLE RESOUCE FILE
2-在其中编写这样的代码:-
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<corners
android:radius="100dp"
/>
<solid
android:color="#FFFFFF"
/>
<size
android:width="100dp"
android:height="100dp"
/>
</shape>
3-打开包含此按钮的 XML 文件并将按钮的背景更改为您之前创建的可绘制对象
推荐阅读
- android-studio-3.0 - 安装 Android Studio 3.2 后出现“AndroidSdkData.getSdkData 不能为空”错误
- python - 如何向量化类实例化以允许 NumPy 数组作为输入?
- java - 使用 xml 布局在 Android 中自定义包装视图
- python - 从仅包含它的 Keras 张量中提取原始值
- php - Simple html dom parser get tr from table
- html - 修复了 HTML 表中的标题
- php - 在 php 中使用 ftp_put() 上传文件重复
- java - jvm可用内存空间越来越小
- java - 带有 Lombok 和 Guice 注入的 Java 链继承
- tensorflow - 如何在 tensorflow 1.11 中使用 keras InceptionV3 模型?