2012-06-13 125 views
0

我在查找部件Switch。不轉換語句或開關活動等。如何實現小部件開關?

在網上有非常罕見的教程。

我的問題是在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> 

在那裏發生的錯誤。

錯誤

Missing styles. Is the correct theme chosen for this layout? Use the Theme combo box above the layout to choose a different layout, or fix the theme style references.

NOTE: This project contains resource errors, so aapt did not succeed, which can cause rendering failures. Fix resource problems first.

Failed to find style 'switchStyle' in current theme java.lang.NullPointerException Exception details are logged in Window > Show View > Error Log

我不知道該怎麼辦呢?

+1

什麼是錯誤? – Christine

+1

如果您只是無法看到開關,您可能需要'Switch'上的'layout_below'屬性才能將它放置在第二個TextView的下面。雖然'layout_alignParentRight'應該讓它可見,如果有空間,所以我們需要更多關於錯誤的信息。 – Nick

+0

我已更新錯誤圖形佈局 –

回答

2

您的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(); 
} 
} 
+2

我正在使用android:minSdkVersion =「18 android:targetSdkVersion =」19「,但仍然是同樣的錯誤,任何建議嗎? – CoDe

+0

你有解決方案相同嗎? – Aakash

-3

使用XML文件的主題定爲Theme.DeviceDefault

+0

這是什麼意思? – JMRboosties

0

你可以使用android .support.v7.widget.SwitchCompat以實現向後兼容。