具有图像背景、文本和状态功能的Android按钮

具有图像背景、文本和状态功能的Android按钮,android,xml,android-layout,background-image,Android,Xml,Android Layout,Background Image,我想在XML布局中创建“按钮”(如果可能的话),它有基于状态的背景图像(该图像不是9-patched)和文本。使用哪个组件 我的按钮_states.xml看起来像: <?xml version="1.0" encoding="UTF-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" andr

我想在XML布局中创建“按钮”(如果可能的话),它有基于状态的背景图像(该图像不是9-patched)和文本。使用哪个组件

我的
按钮_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>
按钮它在没有背景图像的情况下渲染

如何在文本和有状态的“静态”背景下使用“按钮”

Thx


要做到这一点,最简单的方法是用代码。使用(常规)按钮上的
setBackgroundDrawable()
设置所需背景。如果您使用选择器作为提供的绘图工具,您应该可以

[Edit]要使用资源执行此操作,请使用以下代码将您的
按钮\u state.xml
放在res\drawable文件夹中:

Button button = (Button) findViewById(R.id.my_button);
button.setBackgroundResource(R.drawable.button_state);

谢谢。。。我试试。。。是否可以将setBackgroundDrawable()与选择器一起使用?如果是,怎么做?
Button button = (Button) findViewById(R.id.my_button);
button.setBackgroundResource(R.drawable.button_state);