自定义按钮样式出现在预览中但未出现在设备上
Custom Button style is appearing in preview but not on device
我为我的按钮制作了自定义样式。它完美地出现在布局预览中(通过 Android Studio XML 预览)但是当我 运行 应用程序时,按钮没有想要的边框。
知道造成这种情况的原因吗?
activity_main.xml
<style name="Button.Pink" parent="Button">
<item name="android:background">@drawable/rounded_corners_button</item>
<item name="android:backgroundTint">@color/pink</item>
<item name="android:textColor">@color/white</item>
</style>
styles.xml
<Button
style="@style/Button.Pink"
android:id="@+id/btn_sale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top|start"
android:enabled="false"
android:foreground="?attr/selectableItemBackground"
android:text="@string/sale"
android:layout_marginTop="4dp"
android:layout_marginStart="4dp"
android:visibility="gone"
tools:visibility="visible" />
rounded_corners_button.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<corners android:radius="65dp" />
</shape>
</item>
</selector>
编辑:
添加一些渲染器图片
XML 预览布局设计显示的内容:
应用程序显示的内容:
尝试更改此行
<style name="Button.Pink" parent="Button">
到
<style name="Button.Pink" parent="Widget.AppCompat.Button"> //AppCompat style to support backports.
已更新
如果您的目标是最小 api 21,则尝试支持库 28..
中引入的最新 material 按钮
<android.support.design.button.MaterialButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sale"
app:backgroundTint="@color/colorAccent"
app:cornerRadius="50dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp" />
也许您只是忘记删除 android:visibility="gone"? :)))
另外,你为什么要使用 android:backgroundTint
和 android:foreground
?
也许你可以做一些更简单的事情,像这样
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@android:color/"
<corners android:radius="65dp" />
</shape>
并且还为其他状态创建形状,而不是创建一个选择器
使用下面的代码希望对你有帮助
按钮
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:textColor="#ffffff"
android:background="@drawable/rounded_corners_button"
android:text="Buttons" />
rounded_corners_button 在 draweble
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item >
<shape android:shape="rectangle" >
<corners android:radius="5dip" />
<stroke android:width="1dip" android:color="#00FFFF" />
<gradient android:angle="-90" android:startColor="#8dbab3" android:endColor="#58857e" />
</shape>
</item>
</selector>
我为我的按钮制作了自定义样式。它完美地出现在布局预览中(通过 Android Studio XML 预览)但是当我 运行 应用程序时,按钮没有想要的边框。
知道造成这种情况的原因吗?
activity_main.xml
<style name="Button.Pink" parent="Button">
<item name="android:background">@drawable/rounded_corners_button</item>
<item name="android:backgroundTint">@color/pink</item>
<item name="android:textColor">@color/white</item>
</style>
styles.xml
<Button
style="@style/Button.Pink"
android:id="@+id/btn_sale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top|start"
android:enabled="false"
android:foreground="?attr/selectableItemBackground"
android:text="@string/sale"
android:layout_marginTop="4dp"
android:layout_marginStart="4dp"
android:visibility="gone"
tools:visibility="visible" />
rounded_corners_button.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<corners android:radius="65dp" />
</shape>
</item>
</selector>
编辑: 添加一些渲染器图片
XML 预览布局设计显示的内容:
应用程序显示的内容:
尝试更改此行
<style name="Button.Pink" parent="Button">
到
<style name="Button.Pink" parent="Widget.AppCompat.Button"> //AppCompat style to support backports.
已更新
如果您的目标是最小 api 21,则尝试支持库 28..
中引入的最新 material 按钮<android.support.design.button.MaterialButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sale"
app:backgroundTint="@color/colorAccent"
app:cornerRadius="50dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp" />
也许您只是忘记删除 android:visibility="gone"? :)))
另外,你为什么要使用 android:backgroundTint
和 android:foreground
?
也许你可以做一些更简单的事情,像这样
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@android:color/"
<corners android:radius="65dp" />
</shape>
并且还为其他状态创建形状,而不是创建一个选择器
使用下面的代码希望对你有帮助
按钮
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:textColor="#ffffff"
android:background="@drawable/rounded_corners_button"
android:text="Buttons" />
rounded_corners_button 在 draweble
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item >
<shape android:shape="rectangle" >
<corners android:radius="5dip" />
<stroke android:width="1dip" android:color="#00FFFF" />
<gradient android:angle="-90" android:startColor="#8dbab3" android:endColor="#58857e" />
</shape>
</item>
</selector>