我一直在努力寻找资源,解释如何Switch在Material Design主题中设置按钮的样式.
此链接确实解释了颜色值和美学细节,但没有说明如何通过在Material设计主题中设置某些属性来实现此目的.
http://www.google.com/design/spec/components/switches.html#switches-switch
如果没有直接的方法来设置Switch的颜色,那么我可以使用哪些drawable来制作我自己的版本?
在Lollipop之前的Android版本中,有没有办法让开关的拇指高度大于轨道高度?
我本质上是试图在Kitkat中创建一个类似材料设计的开关小部件(和旧版本一样 - 这里的问题是默认情况下拇指'适合'在轨道内).我已经在这方面工作了很长一段时间,但我似乎无法达到正确的设计.
我按照这个SO帖子上的解决方案:如何更改Switch Widget的大小.
任何建议或解决方案将是最受欢迎的.
我正在尝试在Android中自定义“切换”按钮。我有一个问题,我想在轨道上显示开-关文本,而不是在Android上将拇指显示为默认文本。我们可以自定义它吗?
我有一个带有适配器的ListView,其布局有一个Switch,因此列表中的每个项目都有一个开关.
我想使用这段代码:
mySwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// do something, the isChecked will be
// true if the switch is in the on position
}
});
Run Code Online (Sandbox Code Playgroud)
但我不知道在哪里使用它:在Adapter类或List Containing类中,我怎么能知道当我切换那个开关时我修改了什么项,所以我可以修改它的属性.
编辑:这是实现方法的Adapter类.它在每次调用方法getView()时运行,而不是在它应该运行时(当交换机切换时),并且Logs出现在logcat中但是切换的对象没有被修改,当我向下滚动并向上滚动时,开关回到原来的状态:
package com.nahue.actions;
import java.util.ArrayList;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;
/*@SuppressLint("ViewHolder") */public class AdapterActions extends ArrayAdapter<Action>{
// our ViewHolder.
// caches our TextView
static …Run Code Online (Sandbox Code Playgroud) 我试图从AppCompat实现SwitchCompat,但它在不同的版本设备上看起来不同.在棒棒糖和Froyo它看起来不错,但在姜饼到KitKat它看起来不像一个开关.

码:
<android.support.v7.widget.SwitchCompat
android:id="@+id/label_switch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOff="No"
android:textOn="Yes"
android:checked="false" />
Run Code Online (Sandbox Code Playgroud)
我是否可以使这些开关在所有版本中看起来相同或者至少使它们看起来像一个开关?
我的问题是 - 当我点击 a 时<Switch>,它首先被切换,然后OnCheckedChangeListener被调用。
我想要的是这样的:
<Switch>被点击 --> 我显示一个 AlertDialog --> 如果按下是或否 --> 然后用setChecked(boolean)翻转 ` ,[boolean = true 如果按下是,如果按下否则为 false]。
问题:<Switch>单击时,它会自动翻转。我想首先从 AlertDialog 用是或否来限定它。
sw_enableDisable
.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
getActivity());
alertDialogBuilder
.setMessage(
"Sure you want to enable?. ")
.setCancelable(true)
.setPositiveButton(
getString("YES"),
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
dialog.cancel();
}
})
.setNegativeButton( …Run Code Online (Sandbox Code Playgroud) 如果是按钮,则表达式:
yourButton.setOnclicklistener(new .....)
Run Code Online (Sandbox Code Playgroud)
可以用 RxJava 编写,如:
RxView.clicks(yourButton).subscribe(....)
Run Code Online (Sandbox Code Playgroud)
我想知道对于 Android 是否Switch可以使用以下表达式编写RxJava
常规版本:
yourSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
}
});
Run Code Online (Sandbox Code Playgroud)
拉姆达版本:
yourSwitch.setOnCheckedChangeListener((buttonView, isChecked) -> {
});
Run Code Online (Sandbox Code Playgroud)
RxJava 版本???
我读到了为SwitchCompat在Android 5.0中实现Switch小部件而引入的新内容.我尝试使用相同但我无法看到可绘制的拇指图像,如下图所示.

我的XML代码如下,
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp">
<android.support.v7.widget.SwitchCompat
android:id="@+id/sampleSwitch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:showText="false"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="20dp"
android:text="@string/action" />
<TextView
android:id="@+id/switchStatus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/sampleSwitch"
android:layout_marginTop="22dp"
android:text="@string/status"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
我能够在预览设计(eclipse中的图形布局选项卡)中看到上面布局的拇指图像,但是当我运行我的代码时,我看不到图像.
预览设计

这是我得到的例外
java.lang.NullPointerException:尝试
boolean android.graphics.drawable.Drawable.getPadding(android.graphics.Rect)在空对象引用上调用虚方法' '
有人可以帮忙解决问题吗?
我正在寻找仅将此颜色应用于所有开关.但默认情况下,它colorAccent取代了此主题进行切换.
装置:棉花糖.
布局:
<Switch
android:id="@+id/soundSwitch"
style="@style/SwitchStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginBottom="@dimen/large_space"
android:layout_marginRight="@dimen/medium_space"
android:layout_marginTop="@dimen/large_space"
android:checked="true"
/>
Run Code Online (Sandbox Code Playgroud)
款式-V21:
<style name="SwitchStyle" parent="Theme.AppCompat.Light">
<!-- active thumb & track color (30% transparency) -->
<item name="android:colorControlActivated">@color/switch_color</item>
<!-- inactive thumb color -->
<item name="colorSwitchThumbNormal">#f1f1f1</item>
<!-- inactive track color (30% transparency) -->
<item name="android:colorForeground">#42221f1f</item>
</style>
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?