我的styles.xml中有一个切换按钮正在使用的以下样式。但是,我希望切换按钮为fill_parent
,但不拉伸图像。我如何告诉它将背景图像居中而不拉伸它?我正在努力增加用户可以点击的区域。
切换:
<ToggleButton
android:id="@+id/OtherToggle"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
style="@style/OtherToggle" />
样式:
<style name="OtherToggle">
<item name="android:textOn">""</item>
<item name="android:textOff">""</item>
<item name="android:disabledAlpha">?android:attr/disabledAlpha</item>
<item name="android:background">@drawable/other_toggle_bg</item>
</style>
我想你的意思是确实想拉伸图像,但你想保持纵横比不变?
如果是这样的话,试着把这个添加到你的风格中:
<item name="android:scaleType">centerInside</item>
如果centerInside不能满足您的要求,请参阅此处了解其他可能性。
也许您可以使用图层列表作为ToggleButton的背景绘图?创建以下xml文件并在android:background
属性中使用:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+android:id/background"
android:drawable="@android:color/transparent" />
<item>
<bitmap android:src="@drawable/other_toggle_bg"
android:gravity="center" />
</item>
</layer-list>