我试图覆盖默认ToggleButton外观.这是定义以下内容的XML ToggleButton:
<ToggleButton android:id="@+id/FollowAndCenterButton"
android:layout_width="30px"
android:layout_height="30px"
android:textOn="" android:textOff="" android:layout_alignParentLeft="true"
android:layout_marginLeft="5px"
android:layout_marginTop="5px" android:background="@drawable/locate_me"/>
Run Code Online (Sandbox Code Playgroud)
现在,我们有两个30 x 30图标,我们想要用于点击/未点击状态.现在我们有代码根据状态以编程方式更改背景图标:
centeredOnLocation.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (centeredOnLocation.isChecked()) {
centeredOnLocation.setBackgroundDrawable(getResources().getDrawable(R.drawable.locate_me_on));
} else {
centeredOnLocation.setBackgroundDrawable(getResources().getDrawable(R.drawable.locate_me));
}
}
});
Run Code Online (Sandbox Code Playgroud)
显然我正在寻找一种更好的方法来做到这一点.我试图为背景图像制作一个选择器,它会自动在状态之间切换:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/locate_me" /> <!-- default -->
<item android:state_checked="true"
android:drawable="@drawable/locate_me_on" /> <!-- pressed -->
<item android:state_checked="false"
android:drawable="@drawable/locate_me" /> <!-- unchecked -->
Run Code Online (Sandbox Code Playgroud)
但这不起作用; 阅读ToggleButtonAPI(http://developer.android.com/reference/android/widget/ToggleButton.html),看来唯一继承的xml属性是
XML Attributes
Attribute Name Related Method Description
android:disabledAlpha The alpha to apply to the …Run Code Online (Sandbox Code Playgroud) 是否可以在Android中创建一个具有图像但没有文本的切换按钮?理想情况下它看起来像这样:

我看过类似的帖子,答案是改变背景,但我想保留Holo Light布局,只需将文本与图像交换.
我需要能够以编程方式改变图像源,
任何想法我会如何做到这一点?
如果不能这样做,有没有办法可以打开和关闭普通按钮?
有没有人知道是否可以使ImageView可检查.我尝试在我的项目中使用State-list drawable资源,我为ImageView检查状态定义图片,但是没有属性可以使ImageView可检查,只能单击.
也许有人知道解决这个问题的方法吗?
我有一个30px x 30px的png文件,我想用作android:src而不是android:background.通过使用它作为背景,并提到高度和宽度的"wrap_content",最终结果看起来更像是45px 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>
Run Code Online (Sandbox Code Playgroud)
在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>
Run Code Online (Sandbox Code Playgroud)
在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>
Run Code Online (Sandbox Code Playgroud)
在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>
Run Code Online (Sandbox Code Playgroud)
playbutton.png和pausebutton.png各占30px x 30px.但是当我看到它放在设备上时,它们看起来要大得多
使用android,我想得到一个垂直切换小部件实现.据我所知,看起来交换机只有水平方向.我希望得到以下内容:

在浏览了这里的主题并搜索谷歌之后,我还没有找到能够给我这个的东西.我搜索的所有东西都只给我横向实现.
所以我可以生成典型的水平开关
<Switch
android:id="@+id/switch_button"
android:layout_width="130dp"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/label"
android:layout_gravity="center"
android:switchMinWidth="130dp"
android:thumb="@drawable/switch_selector"
android:track="@drawable/track_selector" >
</Switch>
Run Code Online (Sandbox Code Playgroud)
但似乎没有一个好的方法来设置方向.我知道这个问题有点高.是否有一些立即可用的属性允许我进行垂直切换?或者我是否必须创建自定义开关并可能修改onDraw方法以使其垂直翻转?任何帮助表示赞赏.谢谢.