我已经使用Android的Preference API制定了设计设计。
但我不知道如何创建“Master on / off开关”。这里提到http://developer.android.com/design/patterns/settings.html,但没有关于如何实现它的文档。
从技术上讲,它是位于操作栏中的SwitchPreference,它会在关闭时禁用所有子设置。
有什么想法吗?
答案 0 :(得分:6)
你必须手动创建它。这是您应该实现的模式。您可以在操作栏中添加一个按钮,然后单击,您可以启用或禁用该功能并相应地更改绘图。你可以使用switch标签,但这仅在api 14中引入。例如,
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<Switch
android:id="@+id/switchForActionBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
</RelativeLayout>
然后,在mainmenu.xml中添加项目如下
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/myswitch"
android:title=""
android:showAsAction="always"
android:actionLayout="@layout/switch_layout"
/>
</menu>
您也可以添加使用
public class TogglePreference extends Preference {
public TogglePreference(Context context) {
super(context);
}
public TogglePreference(Context context, AttributeSet attrs) {
super(context, attrs);
}
public TogglePreference(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public View getView(View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = new LinearLayout(getContext());
((LinearLayout) convertView)
.setOrientation(LinearLayout.HORIZONTAL);
TextView txtInfo = new TextView(getContext());
txtInfo.setText("Test");
((LinearLayout) convertView).addView(txtInfo,
new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT, 1));
ToggleButton btn = new ToggleButton(getContext());
((LinearLayout) convertView).addView(btn);
}
return convertView;
}
还有preferences.xml:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<PreferenceCategory android:title="Test custom preferences" >
<android.dumdum.TogglePreference />
</PreferenceCategory>
</PreferenceScreen>
答案 1 :(得分:1)
您必须使用开关创建自定义操作栏,然后您才能实现此目的,请参阅此
How to add a switch to android action bar?
和
http://www.vogella.com/articles/AndroidActionBar/article.html