【问题标题】:Android switch custom left-right drawablesAndroid切换自定义左右drawables
【发布时间】:2015-06-05 21:46:38
【问题描述】:

我正在尝试创建一个这样的自定义开关:

我觉得我需要的是一个左/右可绘制对象,每个都处于绿色/白色状态,或者是一个带有可绘制对象的绿色轮廓,用于何时应该选择选项。

this 之类的帖子中,我不明白的是,提供的所有示例可绘制对象如何向右倾斜,而“打开”按钮却向左倾斜。

我正在尝试使用以下“拇指”可绘制对象。

<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>

但它似乎切断了可绘制对象的末端。如果我也将轨道设置为这样的:

<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>

那么我得到的只是一条细线。所以,我不确定下一步该尝试什么。

【问题讨论】:

    标签: android android-switch


    【解决方案1】:

    这不能单独使用样式,这需要自定义视图实现。那是很多代码,所以我对您的建议是使用 3rd 方库,例如 https://github.com/hoang8f/android-segmented-control 这个库已经过试验和测试,因此可以安全地使用它而不是重写它。

    【讨论】:

    • 所以,也许答案是这不是一个特别面向 Android 的设计,设计需要更改以更适合 Android 的 UI 元素
    • 这个设计来源于iOS,但是新的SwitchCompat小部件看起来和这个小部件一样好和简单,所以试试吧。
    【解决方案2】:

    我创建了一个示例应用程序,您可以找到 here 来解决您的问题。你可以看看this 库,它正确实现了段控制。

    基本上,我创建了一个扩展LinearLayout 的自定义控件,默认weightSum 为2。添加了两个Buttonsweight 为1,并应用了一些逻辑在它们之间切换。在切换时,会触发基于此界面构建的自定义事件。

    public interface SwitchToggleListener {
        void onSwitchToggle(SwitchToggleState switchToggleState);
    }
    

    我使用drawable 资源来设置按钮样式。

    最终的结果是这样的

    【讨论】:

      【解决方案3】:

      您可以将 RadioGroup 与两个 RadioButton 一起使用:

      <RadioGroup
                      android:layout_width="match_parent"
                      android:layout_height="wrap_content"
                      android:gravity="center_horizontal"
                      android:orientation="horizontal">
      
                  <RadioButton
                          android:id="@+id/male_radio_button"
                          android:layout_width="wrap_content"
                          android:layout_height="wrap_content"
                          android:textColor="@drawable/selector_radio_btn_text"
                          android:background="@drawable/selector_radio_btn_left_bg"
                          android:gravity="center"
                          android:button="@null"
                          android:text="@string/male_radio_text"
                          />
      
                  <RadioButton
                          android:id="@+id/female_radio_button"
                          android:layout_width="wrap_content"
                          android:layout_height="wrap_content"
                          android:textColor="@drawable/selector_radio_btn_text"
                          android:background="@drawable/selector_radio_btn_right_bg"
                          android:gravity="center"
                          android:button="@null"
                          android:text="@string/female_radio_text"/>
              </RadioGroup>
      

      其中 selector_radio_btn_text.xml 是文本颜色选择器:

      <?xml version="1.0" encoding="utf-8"?>
      <selector xmlns:android="http://schemas.android.com/apk/res/android">
      <item android:state_checked="true" android:color="@color/checkbox_text_color_checked"/>
      <item android:state_checked="false" android:color="@color/checkbox_text_color"/>
      

      而 selector_radio_btn_left_bg(right bg).xml 是左右两边的背景

      【讨论】:

        【解决方案4】:

        这可以通过使用 RadioGroup 和单选按钮轻松完成,而不是使用任何外部 libaray 或支持库。正如“Dimitry”在他的回答中提到的那样,我也用同样的方法来实现。 这是我对How to custom switch button?问题给出的答案的复制粘贴

          <RadioGroup
            android:checkedButton="@+id/offer"
            android:id="@+id/toggle"
            android:layout_width="match_parent"
            android:layout_height="30dp"
            android:layout_marginBottom="@dimen/margin_medium"
            android:layout_marginLeft="50dp"
            android:layout_marginRight="50dp"
            android:layout_marginTop="@dimen/margin_medium"
            android:background="@drawable/pink_out_line"
            android:orientation="horizontal">
        
            <RadioButton
                android:layout_marginTop="1dp"
                android:layout_marginBottom="1dp"
                android:layout_marginLeft="1dp"
                android:id="@+id/search"
                android:background="@drawable/toggle_widget_background"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:button="@null"
                android:gravity="center"
                android:text="Search"
                android:textColor="@color/white" />
        
            <RadioButton
                android:layout_marginRight="1dp"
                android:layout_marginTop="1dp"
                android:layout_marginBottom="1dp"
                android:id="@+id/offer"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="@drawable/toggle_widget_background"
                android:button="@null"
                android:gravity="center"
                android:text="Offers"
                android:textColor="@color/white" />
        </RadioGroup>
        

        pink_out_line.xml

         <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle">
        <corners android:radius="2dp" />
        <solid android:color="#80000000" />
        <stroke
            android:width="1dp"
            android:color="@color/pink" />
        </shape>
        

        toggle_widget_background.xml

              <?xml version="1.0" encoding="UTF-8"         standalone="no"?>
              <selector        xmlns:android="http://schemas.android.com/apk/res/android">
             <item android:drawable="@color/pink" android:state_checked="true" />
             <item android:drawable="@color/dark_pink" android:state_pressed="true" />
             <item android:drawable="@color/transparent" />
            </selector>
        

        【讨论】:

          【解决方案5】:

          更新答案

          为此,您应该在可绘制文件夹中创建图层列表

          res/drawable/toggle_layerlist.xml

              <?xml version="1.0" encoding="utf-8"?>
          <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
              <item android:drawable="@drawable/choice_right">
          <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>
              </item>
          
              </layer-list>
          

          将图层列表添加到您的选择器

          <selector xmlns:android="http://schemas.android.com/apk/res/android">
                  <item android:drawable="@drawable/toggle_layerlist" android:state_checked="true"  />
          
              </selector>
          

          将此选择器用作背景,但请确保在开/关状态下清空文本

          通过设置android:textOff="" android:textOn=""

          <ToggleButton
                      android:id="@+id/chkState"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:background="@drawable/toggle_selector"
                      android:textOff=""
                      android:textOn=""/>
          

          对其他状态做同样的事情

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2018-04-09
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-11-18
            • 1970-01-01
            相关资源
            最近更新 更多