Android的按钮,背景图像和文本和状态的功能 [英] Android button with image background and text and stateful functionality

查看:173
本文介绍了Android的按钮,背景图像和文本和状态的功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建XML布局(如果可能)依据国家有背景图片按钮(这个形象是不是9补丁)和文本。使用哪些组成部分?

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. Which component to use?

我的 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 my_normal 9修补的图像。

my_pressed and my_normal are NON 9-patched images.

现在,如果我使用普通的按钮

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

问候

推荐答案

做到这一点最简单的方法是这样做在code。使用 setBackgroundDrawable()上的(普通)按钮来设置所需的背景。如果您使用您选择的绘制提供你应该罚款。

The easiest way to accomplish this is doing it in code. Use setBackgroundDrawable() on the (regular) Button to set the desired background. If you use your selector as the provided drawable you should be fine.

:要做到这一点使用资源,把你的 button_state.xml 在res \\在使用中可绘制文件夹下code:

To do this using resources, place your button_state.xml in the res\drawable folder in use the following code:

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

这篇关于Android的按钮,背景图像和文本和状态的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆