在冰淇淋三明治中,引入了一个Switch Widget,它显示了On Off Slider.
我像这样添加了Switch:
<Switch
android:layout_width="fill_parent"
android:layout_height="48dp"
android:textStyle="bold"
android:thumb="@drawable/switch_thumb_selector"
android:track="@drawable/switch_bg_selector" />
Run Code Online (Sandbox Code Playgroud)
轨道和拇指绘图是九个补丁图像,可以扩展到所有可能的大小.我希望Switch可以扩展到给定边界内的最大大小,但似乎drawable只是在提供的空间内居中.
是否可以增加Switch的大小以使其看起来更大?
任何人都知道Android的幻灯片切换的任何开源实现.默认的android toggle(ToggleButton)并不漂亮.我正在寻找类似iOS的东西.我应该能够从头开始实现一个.但如果已经有类似的东西,那么我可以在它上面构建.
在此先感谢精彩的stackoverflow社区.
编辑1: 我对iOS Slide Toggle的意思是UISwitch

Edit2:只是想总结一下答案.Commonsware提供了线索.我最终将Switch代码从4.0 移植到2.2.2.由于开源代码,后端移植并不是很困难.代码托管在git hub上.http://github.com/pellucide/Android-Switch-Demo-pre-4.0/tree/master/
该项目的截图

我正在尝试更改Android中交换机的颜色.我意识到我需要新的9件.我去了http://android-holo-colors.com/并选择了我的颜色并选择了(Switch Jelly Bean).要使用Switch Jelly Bean,我必须使用:https://github.com/BoD/android-switch-backport.要将它导入我的项目,我必须添加:
<item name="switchStyle">@style/Widget.Holo.CompoundButton.Switch</item>
到我的样式,然后在xml我必须像这样使用开关:
<org.jraf.android.backport.switchwidget.Switch
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
现在用开关的一切工作正常.接下来,我从android全息颜色生成器输出所有内容并将其放入正确的文件中:
然后我添加到我的xml:
<org.jraf.android.backport.switchwidget.Switch
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:thumb="@drawable/apptheme_switch_inner_holo_light"
android:track="@drawable/apptheme_switch_track_holo_light" />
但它仍然是原始的蓝色.我相信我正在做的一切正确.一切都编译(xml,java).注意:我org.jraf.android.backport.switchwidget.Switch也在我的java中导入.有任何想法吗?
我正在尝试创建这样的自定义开关:

我觉得我需要的是左/右抽屉,每个都是绿色/白色状态,或者是绿色轮廓,带有抽屉,用于选择时.
我不喜欢在帖子明白这是所有样本绘制资源如何"打开"按钮,偏到左边提供向右倾斜,然而.
我正在尝试使用以下'拇指'可绘制.
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:drawable="@drawable/choice_right"/>
<item android:drawable="@drawable/choice_left"/>
</selector>
Run Code Online (Sandbox Code Playgroud)
但它似乎削减了抽签的目的.如果我也设置了这样的轨道:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<stroke
android:width="1dp"
android:color="@color/text_input"/>
<corners
android:radius="1dp"
android:bottomLeftRadius="4.5dp"
android:bottomRightRadius="4.5dp"
android:topLeftRadius="4.5dp"
android:topRightRadius="4.5dp" >
</corners>
</shape>
Run Code Online (Sandbox Code Playgroud)
然后我得到的只是一条细线.所以,我不确定下一步该尝试什么.
我是Android开发的新手,也是Android主题/定制的新手,看起来是一个广泛的主题......
我正在尝试给我的Switch组件一个特殊的外观和感觉,但我无法实现我想要的东西,avan在互联网上查找了很多资源.
这真让我抓狂!!
在此先感谢您的帮助,Android大师!
我在一个现有的android应用程序(v1)上工作,它是minSdkVersion ="8".
因此,应用程序使用第三方库来获取操作栏(ActionBar Sherlock)和开关(SwitchCompatLibrary):
今天,我们正在制作v2版本,minSdkVersion ="14".
我们的客户还要求我们更改默认的开关外观.
这看起来真的像最新的材料设计开关,但用橙色代替"绿 - 蓝"颜色,就像这里一样.
由于我们现在是minSdk 14,我们可以删除2个库"ActionBarSherlock"和"SwitchCompatLibrary",但这不是一个选项.的确,我们没有时间......
实际上,我尝试在我的项目的gradle文件appcompat-v7中添加依赖项,以便尝试使用带有材质主题的本机开关组件(请参阅此处),但这会因为重复的属性定义而提到错误,并提到了2个库以上.所以我不能用它,我不确定这应该有用......
dependencies {
(...)
compile project(':actionbarsherlock')
compile project(':switchCompatLibrary')
compile files('libs/android-support-v4.jar')
// incompatible avec actionbarsherlock and SwitchCompatLibrary...
// compile "com.android.support:appcompat-v7:22.0.+"
(...)
}
Run Code Online (Sandbox Code Playgroud)
所以这是执行代码.
在我的layout.xml文件中,我使用SwitchCompatLibrary开关:
<de.ankri.views.Switch
android:id="@+id/switchButtonNotification"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true" />
Run Code Online (Sandbox Code Playgroud)
在我的themes.xml文件中:
<style name="Theme.Orange" parent="@style/Theme.Sherlock">
...
<!-- Theme switch with SwitchCompatLibrary -->
<item name="switchStyle">@style/switch_light</item>
<item name="textAppearance">@style/TextAppearance</item>
</style>
Run Code Online (Sandbox Code Playgroud)
使用SwitchCompatLibrary本身定义的样式信息
styles.xml:
<resources …Run Code Online (Sandbox Code Playgroud) 我试图改变开关的背景.它正在改变,但问题是一些文字是可见的.
以下是我的代码
<Switch
android:id="@+id/switchsubsurf"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="68dp"
android:padding="5dip"
android:textOff=" "
android:textOn=" "
android:thumb="@drawable/toggle_button"
android:track="@drawable/toggle_bg" />
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用开/关开关制作列表视图.我发现这个简单的代码,但问题是它不适合我.
<Switch
android:id="@+id/itemListSwitch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOn="Vibrate on"
android:textOff="Vibrate off"/>
Run Code Online (Sandbox Code Playgroud)
是的,它显示一个开关,但文本(振动打开/关闭)不是.仅显示可以切换的圆圈.我想发布截图但我不被允许,因为我缺乏声誉.任何人都可以告诉我为什么,因为我试图找到答案,但像上面的一个简单的代码正在为他们工作.如果有帮助,我的Android手机版本是5.0.2.提前致谢!