我怎样才能用这个主题做一个按钮?蓝色发光盒:
最佳答案
这可以通过基于自定义按钮图像创建9-patch image来完成(并且可以选择为选定/按下按钮状态图形)。9-patch格式基本上是一个png文件,它带有特殊的标记像素,这些标记像素将按钮图形分割成若干段,以便它可以根据按钮视图的最终大小动态地增长和收缩。android sdk附带了一个用于创建这些9个补丁图像的工具。
完成后,您需要在drawable
资源目录中创建一个状态列表xml资源文件。该状态列表引用每个按钮状态(正常、按下等)的9个补丁图像。sdk文档很好地解释了这一点:http://developer.android.com/guide/topics/ui/controls/button.html#CustomBackground
代码摘录自该页:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/button_pressed"
android:state_pressed="true" />
<item android:drawable="@drawable/button_focused"
android:state_focused="true" />
<item android:drawable="@drawable/button_default" />
</selector>
最后,您可以将可绘制的XML作为背景图像资源分配给按钮,如下所示:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Some label text"
android:background="@drawable/my_button_state_list" />
关于android - Android Studio - 需要帮助创建按钮,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29528169/