Android的按键采用文字和图片 [英] Android Button with text and image
问题描述
看到很多问题要问此功能,并尝试遵循的答案后,我在想,如果有一个更清晰的例子可以了?
After seeing many questions for this feature and attempting to follow the answers, I was left wondering if there was a clearer example to be had?
编辑:我试图做一个大的按钮有这样的'中间',其中的图片和文字。它必须表现为一个按钮(StateList绘制),图像/文本对应该进行分组和集中(作为一个整体)
I was attempting to make a large button that had an image and text that where 'in the middle'. It had to behave as a button (StateList drawable) and the image/text pair should be grouped and centered (as a group)
推荐答案
在试图挽救别人一些时间,我提供这样的:
In an attempt to save others some time, I offer this:
布局/ some_layout.xml
layout/some_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="@+id/menu_ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
<!-- StateList Drawable to make it look like a button -->
android:background="@drawable/btn_std_holo_states"
<!-- Required so you can click on it like a button -->
android:clickable="true"
<!-- Recommended min height from the guidelines -->
android:minHeight="48dp"
<!-- OnClickEvent definition -->
android:onClick="onClickOk" >
<!-- Compound drawable of graphic and text -->
<TextView
android:id="@+id/txt_ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
<!-- Center both the graphic and text inside the button -->
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
<!-- Draw the graphic to the left of the text -->
android:drawableLeft="@drawable/ic_ok"
<!-- Space between the graphic and the text-->
android:drawablePadding="16dp"
<!-- ensures the text and graphic are both centered vertically -->
android:gravity="center"
<!-- Text of the button -->
android:text="@android:string/ok"
<!-- Change the font to match the standard button settings (optional) -->
android:textAppearance="?android:attr/textAppearanceButton" />
</RelativeLayout>
绘制/ btn_std_holo_states.xml(上面提到的)
drawable/btn_std_holo_states.xml (referenced above)
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/abs__btn_cab_done_pressed_holo_dark" android:state_pressed="true"/>
<item android:drawable="@drawable/abs__btn_cab_done_focused_holo_dark" android:state_enabled="true" android:state_focused="true"/>
<item android:drawable="@android:color/transparent" android:state_enabled="true"/>
<item android:drawable="@android:color/transparent"/>
</selector>
请注意:不同的@drawable和@android:这里的颜色设置,可以是任何东西,而只是提供了做一个完整的例子
NOTE: the different @drawable and @android:color settings here can be anything and are only provided to make a complete example
这篇关于Android的按键采用文字和图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!