设置 ToggleButton 的图像源
我有一个 30px x 30px png 文件,我想将其用作 android:src 而不是 android:background。通过使用它作为背景,并在高度和宽度上提及“wrap_content”,最终结果看起来更像是 45 像素 x 45 像素。
理想情况下,我需要的是 ImageButton 和 ToggleButton 的混合。我希望具有设置 ImageButton 的 android:src 的功能以及自动切换状态保存位置的功能。
有什么解决方案或解决方法吗?
TIA。
嗨,它似乎对创建另一种风格没有帮助。 src 看起来仍然比原来的 30px x 30px 大得多。
在main.xml中:
<ToggleButton android:id="@+id/blah"
style="@style/myToggle"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ToggleButton>
在Styles.xml中
<style name="myToggle">
<item name="android:textOn">""</item>
<item name="android:textOff">""</item>
<item name="android:disabledAlpha">?android:attr/disabledAlpha</item>
<item name="android:background">@drawable/my_btn_toggle_bg</item>
</style>
在my_btn_toggle_bg.xml中
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/myBackground" android:drawable="@android:drawable/btn_default_small" />
<item android:id="@+id/myToggle" android:drawable="@drawable/my_btn_toggle" />
</layer-list>
在my_btn_toggle.xml中
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="false" android:drawable="@drawable/pausebutton" />
<item android:state_checked="true" android:drawable="@drawable/playbutton" />
</selector>
playbutton.png和pausebutton.png各为30px x 30px。但当我在设备上看到它时,它们看起来要大得多
I have a 30px x 30px png file that i would like to use as an android:src rather than an android:background. By using it as the background, and mentioning "wrap_content" for both the height and the width, the final result looks more like a 45px x 45 px.
Ideally, what i need is a mix of ImageButton and ToggleButton. I would like to have the feature of setting an android:src of ImageButton and the feature of toggling where states are saved automatically for me.
Any solution or workaround?
TIA.
Hi, It does not seem to help creating another style. The src still appears much bigger that it's original 30px x 30px.
In main.xml:
<ToggleButton android:id="@+id/blah"
style="@style/myToggle"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ToggleButton>
In Styles.xml
<style name="myToggle">
<item name="android:textOn">""</item>
<item name="android:textOff">""</item>
<item name="android:disabledAlpha">?android:attr/disabledAlpha</item>
<item name="android:background">@drawable/my_btn_toggle_bg</item>
</style>
In my_btn_toggle_bg.xml
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/myBackground" android:drawable="@android:drawable/btn_default_small" />
<item android:id="@+id/myToggle" android:drawable="@drawable/my_btn_toggle" />
</layer-list>
In my_btn_toggle.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="false" android:drawable="@drawable/pausebutton" />
<item android:state_checked="true" android:drawable="@drawable/playbutton" />
</selector>
playbutton.png and pausebutton.png are 30px x 30px each. But they appear much bigger when i see it laid out on the device
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以将切换按钮设置为您想要的任何外观。您只需要创建一种样式。查看 /platforms//data/res/values/styles.xml 并搜索 Widget.Button.ToggleButton 它看起来像这样:
因此,如果您找到 btn_toggle_bg 可绘制对象,您会发现:
这表明它使用标准
Button
背景和一个名为 btn_toggle 的可绘制对象用于 src。 btn_src.xml 如下所示:这是用于显示按钮状态的两个可绘制对象。它们实际上被称为 btn_toggle_{on/off}.9.png,因为它们是 9 个补丁图像,因此它们会拉伸以匹配按钮大小。
You can make the toggle button look like whatever you want. You just need to create a style. Have a look in the /platforms//data/res/values/styles.xml and search for Widget.Button.ToggleButton It looks like this:
So if you go find the btn_toggle_bg drawable you find this:
Which shows you that it uses the standard
Button
background and a drawable called btn_toggle for the src. btn_src.xml looks like this:Which are the two drawables that are used to show the state of the button. They are actually called btn_toggle_{on/off}.9.png since they are 9 Patch images so they stretch to match the button size.
我想出了如何使用 ScaleDrawable:
将 ToggleButton 设置为使用选择器 Drawable,像平常一样:
然后,为上面的每个项目的可绘制对象创建一个 ScaledDrawable,它指向您的实际图像文件。例如 ic_star_selected_centered.xml:
最后,Android 中有一个 错误,使任何 ScaledDrawables 首先不可见,直到您设置了他们的级别。因此,在膨胀视图时,请从 1-10000 调用 setLevel()(1 是最小比例,10000 是全尺寸):
I figured out how to do this (centering a smaller background image inside a large ToggleButton) using ScaleDrawable:
Set your ToggleButton to use a selector Drawable, as normal:
Then, make a ScaledDrawable for each of the items' drawables above, which points to your ACTUAL image file. e.g. ic_star_selected_centered.xml:
Finally, there is a bug in Android that makes any ScaledDrawables invisible at first, until you set their level. So when inflating your view, call setLevel() from 1-10000 (1 is smallest scale, 10000 is full-sized):
除了@caseyB所说的(看评论)之外,我还必须改变活动的主题才能达到预期的效果。我认为这是 HoneyComb Xoom 上的问题,在手机上可能不会出现。
感谢大家看我的问题并提供帮助。
In addition to what @caseyB had to say (look at the comments), i had to change the theme of the activity to achieve the intended effect. I think this is an issue on HoneyComb Xoom and may not be seen on phones.
Thanks for everyone for looking at my question and helping.
实现此目的的“快速而肮脏”的方法是使用相对布局并将 ImageView 放置在 ToggleButton 的顶部。设置 android:layout_centerInParent="true" 并在切换开关上设置 onClick 侦听器
说 - CaseyB 的另一个解决方案(自定义背景文件)是正确的方法
"Quick and dirty" way of achieving this is to use relative layout and place your ImageView on top of the ToggleButton. Set
android:layout_centerInParent="true"
and set your onClick listener on the toggleSaid that - the other solution by CaseyB (customizing background files) is the right approach