[英]Android button with image background and text and stateful functionality
I want to create "button" in XML layout (if possible) which has background image based on state (this image is NOT 9-patched) and with text. 我想在XML布局中创建“按钮”(如果可能),该按钮具有基于状态的背景图像(此图像未进行9修补)并带有文本。 Which component to use? 使用哪个组件?
My button_states.xml
looks like: 我的button_states.xml
看起来像:
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@drawable/my_pressed" />
<item android:drawable="@drawable/my_normal" />
</selector>
my_pressed
and my_normal
are NON 9-patched images. my_pressed
和my_normal
是非 9打补丁图像。
Now if I use regular button 现在,如果我使用常规按钮
<Button
android:id="@+id/my_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/button_states"
android:text="@string/my_text" />
button it is rendered without background image. 按钮,它将呈现为没有背景图像。
How to do "button" with text and stateful "static" backgrounds? 如何在文本和有状态的“静态”背景下进行“按钮”操作?
Thx 谢谢
Regards 问候
The easiest way to accomplish this is doing it in code. 完成此操作的最简单方法是在代码中完成。 Use setBackgroundDrawable()
on the (regular) Button to set the desired background. 在(常规)按钮上使用setBackgroundDrawable()
设置所需的背景。 If you use your selector as the provided drawable you should be fine. 如果将选择器用作提供的可绘制对象,则应该没问题。
[Edit] To do this using resources, place your button_state.xml
in the res\\drawable folder in use the following code: [编辑]要使用资源执行此操作,请使用以下代码将button_state.xml
放入res \\ button_state.xml
文件夹中:
Button button = (Button) findViewById(R.id.my_button);
button.setBackgroundResource(R.drawable.button_state);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.