Android的设置的ImageButton的切换Android、ImageButton

2023-09-03 20:38:37 作者:闭加锁、先生

我怎么能有一个ImageButton的停留在'pressed的状态被点击时?基本上,我只是希望背景是德pressed背景,但我想不出如何设置。此刻,我刚才复制的选择按钮的背景到我的资源文件夹,但是当我将它设置为背景,它变得模糊(由于原始图像大于按钮本身)。

How can I have an imagebutton stay in the 'pressed' state when it is clicked? Basically I just want the background to be the depressed background, but I can't figure out how to set it. At the moment, I've just copied the selected button background into my res folder, but when I set it as the background, it becomes blurry (since the original image is bigger than the button itself).

普通背景: 会得到什么:

Normal Background: What I'm getting:

此外,我不相信,我居然可以用这种方法考虑到许多不同的UI布局。该按钮应保持pssed根据用户正在使用的用户界面$ P $。

Also I don't believe I can actually use this method considering the many different UI layouts. The button should stay pressed as per the UI the user is using.

推荐答案

有这样几个方面:

首先,你可以简单地使用的ImageButton ,并手动切换其图像绘制在点击Java编写的。这是在Android does为'洗牌'按钮的,例如。虽然你不会在按钮的背景在它的选中状态的控制,你就可以换出的图像,这可能是从一个Android用户界面一致性的角度来看有利的。

First, you can simply use an ImageButton, and manually toggle its image drawable on click in Java. This is what the stock Music player on Android does for the 'shuffle' button, for example. Although you won't have control over the button background in its checked state, you'll be able to swap out the image, which may be favorable from an Android UI-consistency perspective.

另一种选择是使用一组可绘制和九补丁复杂进去的图像的切换按钮,系统与改变的背景和/或图像的选择在资源切换。这就是选择,我会告诉如下。但是请记住,保持谨慎的用户界面的一致性在此之前。

Another option is to use a complex set of drawables and nine-patches to get an image inside a ToggleButton, with the option of changing the background and/or the image resource upon toggle. That's the option I'll show below. But remember, be cautious about UI consistency before doing this.

RES /布局/ foo.xml

...
<ToggleButton
    android:textOn="" android:textOff=""
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/shuffle_button" />
...

RES /绘制/ shuffle_button.xml

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- use "@android:drawable/btn_default" to keep consistent with system -->
    <item android:drawable="@drawable/toggle_button_background" />
    <item android:drawable="@drawable/shuffle_button_image" />
</layer-list> 

RES /绘制/ toggle_button_background.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- checked state -->
    <item android:state_pressed="false" android:state_checked="true"
          android:drawable="@drawable/btn_default_checked" />

    <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>

RES /绘制/ shuffle_button_image.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/ic_mp_shuffle_on_btn" android:state_checked="true" />
    <item android:drawable="@drawable/ic_mp_shuffle_off_btn" />
</selector>

图片文件

btn_default_&LT;国家&GT; .9.png 可以在找到frameworks/base.git在core/res/res/drawable-hdpi和core/res/res/drawable-mdpi (也LDPI)。 警告:如果您使用这些,你的应用程序看起来与自定义的操作系统的用户界面(即HTC的Sense UI)设备不一致 ic_mp_shuffle_&LT;国家&GT; _btn.9.png 必须是九补丁,使图像得到居中,而不是拉伸以适应按钮。下面是例如华电国际版本的图标: btn_default_<state>.9.png can be found in frameworks/base.git under core/res/res/drawable-hdpi and core/res/res/drawable-mdpi (also ldpi). WARNING: if you use these, your app will look inconsistent on devices with customized OS UIs (i.e. HTC's Sense UI). ic_mp_shuffle_<state>_btn.9.png need to be nine-patches, so that the image gets centered and not stretched to fit the button. Below are example hdpi versions of the icon:

RES / drawable-(H | M | LDPI)/ ic_mp_shuffle_(ON | OFF)_btn.9.png

最后请注意:记得在可能的情况是在系统UI保持一致,同时也要注意的是,你的应用程序可以运行在与操作系统有不同的图形用户界面元素的定制版本的设备如按钮。这方面的一个例子是的HTC Sense ,其中有绿色按钮代替灰色/橙色/黄色的在普通的Andr​​oid。所以,如果你最终抄袭开源库中的 btn_default _... PNG文件创建一个关联性能够按钮的背景,你会打破这些设备上的一致性。

Final Note: Remember to be consistent with the system UI when possible, and be mindful of the fact that your app may run on devices with customized versions of the OS that have different graphics for UI elements like buttons. An example of this is HTC Sense, which has green buttons in place of the grey/orange/yellow ones in stock Android. So, if you end up copying the btn_default_... PNG files from the open source repository to create a toggle-able button background, you'll break consistency on those devices.