相关疑难解决方法(0)

Android:使用XML为togglebutton指定两个不同的图像

我试图覆盖默认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)

java xml android togglebutton

99
推荐指数
1
解决办法
8万
查看次数

切换问题 - 只有一个选项可见

我想在Android应用程序中使用switch.我尝试过,但主要问题是

  • 如果我选择了ON,它将不会显示文本.
  • 如果我选择了OFF,它将不会显示在文本上.

不显示关闭文本但我们可以通过单击开关的黑色区域来选择关闭.

<Switch android:id="@+id/mySwitch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="48dp"
android:layout_marginTop="26dp"
android:height="50dp"
android:text="ON OFF"
android:textSize="20sp"
android:switchMinWidth="50sp"
android:switchPadding="50sp"/>
Run Code Online (Sandbox Code Playgroud)

论国家

在此输入图像描述

关闭状态

在此输入图像描述

我如何能同时显示两个文本?我可以更改状态ON/OFF的开关文本吗?

任何帮助,将不胜感激.

提前致谢.

android switch-statement

7
推荐指数
1
解决办法
3479
查看次数

标签 统计

android ×2

java ×1

switch-statement ×1

togglebutton ×1

xml ×1