Android 如何实现小部件开关?

Android 如何实现小部件开关?,android,switch-statement,Android,Switch Statement,我在找小工具。不切换语句或切换活动等 网上有非常罕见的教程 我的问题是xml中的图形布局无法看到切换 我的xml <RelativeLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_margin="10dp" > <TextView android:id="@+id/text"

我在找小工具。不切换语句或切换活动等

网上有非常罕见的教程

我的问题是xml中的图形布局无法看到切换

我的xml

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_margin="10dp" >

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="推送通知"
        android:textColor="#ffffff"
        android:textSize="20dp" />

    <TextView
        android:id="@+id/status"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/text"
        android:layout_marginTop="5dp"
        android:textColor="#ffffff"
        android:textSize="15dp" />

    <Switch
        android:id="@+id/switch"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:textOff="关"
        android:textOn="开" />
</RelativeLayout>

错误发生在那里

错误

缺少样式。是否为此布局选择了正确的主题?
使用版面上方的主题组合框选择其他版面,或修复主题样式引用。

注意:此项目包含资源错误,因此aapt未成功,这可能导致渲染失败。首先解决资源问题。

在当前主题中找不到样式“switchStyle”
java.lang.NullPointerException
异常详细信息记录在窗口>显示视图>错误日志中


我不知道怎么做?

使用xml文件的主题作为
主题。DeviceDefault
您的AndroidManifest.xml应该有

 <uses-sdk
    android:minSdkVersion="14"
    android:targetSdkVersion="16" />

因为Switch widget仅支持14及以上的API级别

给你举个简单的例子

main.xml
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

            <Switch
                android:id="@+id/bluetooth"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="32dip"
                android:text="Bluetooth"
                />

</GridLayout>

ur .java



package time.pass;

import android.os.Bundle;
import android.app.Activity;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.Toast;

public class TimePass extends Activity implements CompoundButton.OnCheckedChangeListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_time_pass );

    Switch s = (Switch) findViewById(R.id.bluetooth);
    if (s != null) {
        s.setOnCheckedChangeListener(this);
    }
}


@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
   Toast.makeText(this, "Bluetooth is " + (isChecked ? "on" : "off"),
           Toast.LENGTH_SHORT).show();
}
}
main.xml
java
打包时间。通过;
导入android.os.Bundle;
导入android.app.Activity;
导入android.widget.CompoundButton;
导入android.widget.Switch;
导入android.widget.Toast;
公共类TimePass扩展活动实现CompoundButton.OnCheckedChangeListener{
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u time\u pass);
开关s=(开关)findViewById(R.id.bluetooth);
如果(s!=null){
s、 setOnCheckedChangeListener(此);
}
}
@凌驾
检查更改后的公共无效(复合按钮视图,布尔值已检查){
Toast.makeText(此“蓝牙是”+(已检查?“打开”:“关闭”),
吐司。长度(短)。show();
}
}

您可以使用android.support.v7.widget.SwitchCompat实现向后兼容性。

如果您只是看不到开关,您可能需要在
开关上的
布局属性下方
才能将其定位在第二个文本视图下方。虽然
布局\u alignParentRight
应该在有空间的情况下使其可见,因此我们需要更多关于错误的信息。我已经更新了图形布局中的错误。我正在使用android:minSdkVersion=“18 android:targetSdkVersion=“19”但仍然是相同的错误,有什么建议吗?您有相同的解决方案吗?