按照此文章我能够创建年代提出了图像的切换按钮。我拨动可是没有任何文字,只开/关的图像。
在创建我的切换按钮,它被拉伸并失去它的地步,我怎么让它保留它的原始尺寸?
这些都是图像即时通讯使用:
是
在code:
main.xml中:的
<切换按钮
机器人:ID =@ + ID / changeNumerals
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_alignParentLeft =真
机器人:layout_alignParentTop =真
机器人:检查=真
机器人:背景=@可绘制/ toggle_bg
机器人:纹元=
机器人:textOff =
/>
绘制/ toggle.xml:的
< XML版本=1.0编码=UTF-8&GT?;
<选择的xmlns:机器人=http://schemas.android.com/apk/res/android>
<项目安卓state_checked =假机器人:可绘制=@可绘制/ toggle_off/>
<项目安卓state_checked =真正的机器人:可绘制=@可绘制/ toggle_on/>
< /选择器>
* 绘制/ toggle_bg.xml:*
< XML版本=1.0编码=UTF-8&GT?;
<层列表的xmlns:机器人=http://schemas.android.com/apk/res/android>
<项目机器人:ID =@ +安卓ID /背景机器人:可绘制=@机器人:彩色/透明/>
<项目机器人:ID =@ +安卓ID /切换机器人:可绘制=@可绘制/切换/>
< /层列表>
解决方案
尺寸是硬codeD,但他们会受你的屏幕的大小。
因此XML可能看起来像:
机器人:layout_width =64dp
机器人:layout_height =24dp
对于DPS的官方文档是这里
By following this article I was able to create a toggle button that's made out of images. My toggle doesnt have any text, just on/off images.
When my toggle button is created it's being stretched and loses it's proportions, how do I make it retain it's original size?
These are the images im using:
The code:
main.xml:
<ToggleButton
android:id="@+id/changeNumerals"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:checked="true"
android:background="@drawable/toggle_bg"
android:textOn=""
android:textOff=""
/>
drawable/toggle.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="false" android:drawable="@drawable/toggle_off" />
<item android:state_checked="true" android:drawable="@drawable/toggle_on" />
</selector>
*drawable/toggle_bg.xml:*
<?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 android:id="@+android:id/toggle" android:drawable="@drawable/toggle" />
</layer-list>
解决方案
The dimensions are "hardcoded" but they'll scale with the size of your screen.
So the xml could look like:
android:layout_width="64dp"
android:layout_height="24dp"
The official documentation for dps is here