简体   繁体   English

是否可以在Android中为多个按钮状态使用单个xml?

[英]Is to possible to have a single xml for multiple button states in android?

I have 6 image buttons in my activity each having different images.. 我的活动中有6个图像按钮,每个按钮都有不同的图像。

For those 6 image buttons I am having 6 xml of button states in drawable folder for each button. 对于这6个图像按钮,我在每个按钮的drawable文件夹中具有6个按钮状态的xml。

Is there any way or method so that I can club all the 6 xml into 1 single xml? 有什么方法可以使所有6个xml合并为1个单个xml?

Yes, you can. 是的你可以。 You need not have 6 xmls for 6 different images, and then one more xml for combining the 6 XML files. 您不必为6个不同的图像设置6个xml,然后再为合并6个XML文件又需要一个xml。

You can just have one XML, that contains all the state images. 您只能有一个XML,其中包含所有状态图像。

Check this sample. 检查此样本。

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_window_focused="false" android:state_enabled="true"
        android:drawable="@drawable/btn_default_normal" />
    <item android:state_window_focused="false" android:state_enabled="false"
        android:drawable="@drawable/btn_default_normal_disable" />
    <item android:state_pressed="true" 
        android:drawable="@drawable/btn_default_pressed" />
    <item android:state_focused="true" android:state_enabled="true"
        android:drawable="@drawable/btn_default_selected" />
    <item android:state_enabled="true"
        android:drawable="@drawable/btn_default_normal" />
    <item android:state_focused="true"
        android:drawable="@drawable/btn_default_normal_disable_focused" />
    <item
        android:drawable="@drawable/btn_default_normal_disable" />
</selector>

As you can see, in this single xml, you can refer to the images(drawables), directly. 如您所见,在此单个xml中,您可以直接引用图像(可绘制对象)。

android:drawable="@drawable/btn_default_selected"

please read: http://developer.android.com/training/improving-layouts/reusing-layouts.html 请阅读: http : //developer.android.com/training/improving-layouts/reusing-layouts.html

As to findViewById(), you will have sub-views with identical ids; 至于findViewById(),您将拥有具有相同ID的子视图; to find these views, you will have to find the root view and use that view to find-by-id its child view. 要找到这些视图,您将必须找到根视图,并使用该视图按ID查找其子视图。 (You can eg use a LinearLayout as a button and this is how you can have multiple such buttons.) (例如,您可以将LinearLayout用作按钮,这就是您可以拥有多个此类按钮的方式。)

By the way, if the xml is a drawable, you can just reference that drawable from all buttons. 顺便说一句,如果xml是可绘制的,则可以只从所有按钮引用该可绘制的对象。

UPDATE (yes I do know the above does not answer the question after the question is edited): 更新(是的,我知道在编辑问题后,上面的内容无法回答问题):

I used the same background drawable (a selector) for several buttons. 我为多个按钮使用了相同的背景可绘制对象(选择器)。 The foreground was text (in fact, a LinearLayout). 前景是文本(实际上是LinearLayout)。 I'd suggest you try to reuse the same background drawable. 我建议您尝试重用相同的背景可绘制对象。

Please note that you can have images even on a text button, there are setCompoundDrawablesWithIntrinsicBounds(Drawable left, Drawable top, Drawable right, Drawable bottom) and setCompoundDrawablesWithIntrinsicBounds(int left, int top, int right, int bottom) . 请注意,即使在文本按钮上也可以有图像,包括setCompoundDrawablesWithIntrinsicBounds(左可绘制,顶部可绘制,右侧可绘制,底部可绘制)setCompoundDrawablesWithIntrinsicBounds(左侧,内部顶部,右侧,右侧)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM