我想为我的应用程序设置Lollipop样式开关按钮:

我怎么能实现这个按钮所以在旧版本的android上看起来也是如此?
自定义形状开关看起来像这样:
以上API 21
API 21以下
看起来像<size/>块不适<shape/>用于21之前的API.
任何想法如何解决这个问题?
码
container.xml中:
<Switch
android:id="@id/switch_follow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:textOff=""
android:textOn=""
android:thumb="@drawable/switch_selector"
android:track="@drawable/switch_track"/>
Run Code Online (Sandbox Code Playgroud)
绘制/ switch_selector.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<layer-list>
<item
android:bottom="@dimen/switch_selector_padding"
android:left="@dimen/switch_selector_padding"
android:right="@dimen/switch_selector_padding"
android:top="@dimen/switch_selector_padding">
<shape
android:dither="true"
android:shape="oval"
android:useLevel="false"
android:visible="true">
<gradient
android:angle="270"
android:endColor="@color/primary_white"
android:startColor="@color/primary_white"/>
<corners
android:radius="@dimen/switch_radius"/>
<size
android:width="@dimen/switch_track_height"
android:height="@dimen/switch_track_height" />
</shape>
</item>
</layer-list>
</item>
</selector>
Run Code Online (Sandbox Code Playgroud)
绘制/ switch_track.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:dither="true"
android:shape="rectangle"
android:useLevel="false"
android:visible="true">
<gradient
android:angle="270"
android:endColor="@color/primary_yellow_dark_v2"
android:startColor="@color/primary_yellow_dark_v2"/>
<corners android:radius="@dimen/switch_radius" />
<stroke
android:width="@dimen/switch_stroke_height"
android:color="@android:color/transparent">
</stroke> …Run Code Online (Sandbox Code Playgroud) 这两个对象(SwitchCompat和SwitchMaterial)之间有什么区别?我已经尝试过它们并且在视觉上它们是相同的。
顺便问一下,他们为什么要删除这个Switch类?您知道将来应该用哪个 UI 元素来替换它吗?
android lint android-appcompat android-switch material-components-android
由于以下错误,我无法在我的项目中插入Switch:
查看需要API级别14(当前最小值为8):
但在我的项目属性中,我使用的是Platform 4.1和API Level 16.那么出了什么问题?
我正在尝试创建这样的自定义开关:

我觉得我需要的是左/右抽屉,每个都是绿色/白色状态,或者是绿色轮廓,带有抽屉,用于选择时.
我不喜欢在帖子明白这是所有样本绘制资源如何"打开"按钮,偏到左边提供向右倾斜,然而.
我正在尝试使用以下'拇指'可绘制.
<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)
然后我得到的只是一条细线.所以,我不确定下一步该尝试什么.
我正在使用自定义布局(文本视图和切换)和自定义适配器构建简单的回收站视图.
我的问题是我无法处理交换机的点击事件(这使其切换状态),我已经为RecyclerView它构建了一个触摸监听器,每次点击它都会触发Switch.
经过大量试验后,开关触摸监听器也会触发,但如果单击该开关,我想禁用recyclelerView的触摸事件.
这是我的布局代码:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="60.0dip"
android:layout_weight="1.0"
android:orientation="horizontal"
android:paddingLeft="16dp">
<TextView
android:id="@+id/category_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="false"
android:layout_centerVertical="true"
android:layout_gravity="center_vertical"
android:text="Category Name"
android:textAppearance="?android:textAppearanceLarge" />
<android.support.v7.widget.SwitchCompat
android:id="@+id/category_switch"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_gravity="center_vertical"
android:checked="true"
android:clickable="true"
android:focusable="false"
android:focusableInTouchMode="false"
android:paddingRight="16dp" />
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
这是我的适配器代码:
public CategoryViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View row = layoutInflater.inflate(R.layout.single_row_category, parent, false);
CategoryViewHolder holder = new CategoryViewHolder(row);
return holder;
}
public void onBindViewHolder(CategoryViewHolder holder, final int position) {
AlarmCategory thisCat = categories.get(position);
holder.catName.setText(thisCat.getCategoryName());
holder.catStatus.setChecked(thisCat.isOn());
holder.catStatus.setOnTouchListener(new View.OnTouchListener() …Run Code Online (Sandbox Code Playgroud) How can I design custom switch in android as like in below image:
When it's turned on, it needs to look like this
I also need to show toggle animation effects while switching between two categories. How Can I achieve it? Is there any 3rd party SDK or libraries available? Currently I have designed it with a custom linear layout as like this:
my_layout.xml:
<LinearLayout
android:id="@+id/customSliderLayout"
android:layout_width="@dimen/_200sdp"
android:layout_height="@dimen/_39sdp"
android:layout_centerInParent="true"
android:orientation="horizontal"
android:weightSum="2"
android:background="@drawable/oval_layout_bg"
android:layout_centerHorizontal="true">
<Button
android:id="@+id/userBtn"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:textAllCaps="false"
android:text="@string/toggle_user" …Run Code Online (Sandbox Code Playgroud) 在Android 5.0中点击我的应用程序中的开关时,应用程序崩溃并显示如下所示的logcat.logcat不会在其中的任何位置引用我的代码,并且此开关在所有先前版本上都运行良好.除了背景颜色外,开关看起来不可见,只有在点击后才会崩溃.
经过测试,无论我是否定义了setOnCheckedChangeListener函数,都会发生同样的事情.即使开关在布局中但从未在代码中,它仍然会在单击时崩溃.
其中一个开关:
<Switch
android:layout_width="90dp"
android:layout_height="wrap_content"
android:textOff="Airborn"
android:textOn="Direct Contact"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="@+id/switchWind"/>
Run Code Online (Sandbox Code Playgroud)
这是我如何引用它.
// Doesn't actually matter since same thing happens without this
Switch sWind = (Switch) findViewById(R.id.switchWind);
sWind.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton v, boolean isChecked) {
// Won't reach here
}
});
Run Code Online (Sandbox Code Playgroud)
logcat的:
11-17 22:09:18.722 30190-30190/com.egondev.android.patientzerotest E/InputEventReceiver? Exception dispatching input event.
11-17 22:09:18.722 30190-30190/com.egondev.android.patientzerotest E/MessageQueue-JNI? Exception in MessageQueue callback: handleReceiveCallback
11-17 22:09:18.735 30190-30190/com.egondev.android.patientzerotest E/MessageQueue-JNI? java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.graphics.drawable.Drawable.getPadding(android.graphics.Rect)' on a null object reference
at …Run Code Online (Sandbox Code Playgroud) 我正在尝试创建一个自定义开关按钮,其拇指约占其轨道的75%(宽度).我不想使用图像,只是可绘制的形状,样式等.到目前为止,我已经完成了以下工作:
activity_main.xml中
<Switch
android:id="@+id/onOffSwitch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:track="@drawable/switch_custom_track"
android:thumb="@drawable/switch_custom_thumb"
android:showText="true"
android:textOff="Off"
android:textOn="On"/>
Run Code Online (Sandbox Code Playgroud)
switch_custom_track.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:drawable="@drawable/switch_custom_track_on" />
<item android:state_checked="false" android:drawable="@drawable/switch_custom_track_off" />
</selector>
Run Code Online (Sandbox Code Playgroud)
switch_custom_track_on.xml
<shape
android:shape="rectangle"
xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="20dp" />
<solid android:color="#00b800" />
<size android:width="90dp" android:height="40dp" />
</shape>
Run Code Online (Sandbox Code Playgroud)
switch_custom_track_off.xml
<shape
android:shape="rectangle"
xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="20dp" />
<solid android:color="#fd491d" />
<size android:width="90dp" android:height="40dp" />
</shape>
Run Code Online (Sandbox Code Playgroud)
switch_custom_thumb.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:drawable="@drawable/switch_custom_thumb_on" />
<item android:state_checked="false" android:drawable="@drawable/switch_custom_thumb_off" />
</selector>
Run Code Online (Sandbox Code Playgroud)
switch_custom_thumb_on.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<corners android:radius="20dp" />
<solid android:color="#ffffff" />
<size android:width="70dp" …Run Code Online (Sandbox Code Playgroud) 我正在以编程方式创建一个切换按钮.我遇到的问题是该按钮不显示ON / OFF文本.这是创建代码:
final RelativeLayout.LayoutParams ll = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
ll.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
sw.setLayoutParams(ll);
sw.setTextOn(values[0].getName());
sw.setTextOff(values[1].getName());
values[0].getName() returns "OK", and values[1].getName() returns "NOK"
Run Code Online (Sandbox Code Playgroud)
可能会发生什么?
谢谢Jaime
android ×10
android-switch ×10
xml ×2
api ×1
lint ×1
material-components-android ×1
native ×1
touch-event ×1