我想要添加一个类似于果冻豆原生外观的按钮开关。(视图顶部的蓝/灰色开关)
文档展示了如何在那里创建菜单或添加图标,但没有说明如何添加自定义元素,例如开关。 http://developer.android.com/guide/topics/ui/actionbar.html
我想要添加一个类似于果冻豆原生外观的按钮开关。(视图顶部的蓝/灰色开关)
文档展示了如何在那里创建菜单或添加图标,但没有说明如何添加自定义元素,例如开关。 http://developer.android.com/guide/topics/ui/actionbar.html
创建一个名为 switch_layout.xml
的开关布局。菜单的自定义布局应始终使用 RelativeLayout
。
<?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>
在你的活动中,像往常一样填充 mainmenu.xml
。
getMenuInflater().inflate(R.menu.mainmenu, menu);
return true;
最后我终于找到问题所在了:如果你正在使用新的AppCompat,那么在Switch布局中你应该使用android.support.v7.widget.SwitchCompat
而不是Switch
,否则它将不会显示在ActionBar
上(假定您也在使用AppCompat ActionBar),嗯,actionLayout属性不起作用,必须在代码中设置。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/switchView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<android.support.v7.widget.SwitchCompat
android:id="@+id/switchForActionBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="" />
</RelativeLayout>
然后在代码中设置布局:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
MenuItem item = menu.findItem(R.id.on_off_switch);
item.setActionView(R.layout.on_off_switch);
return true;
}
SwitchCompat
的另一个优点是:开关显示出了更加时尚的外观(例如材料设计)。 - DenisGLandroid:orientation="horizontal"
? - zackygauravRelativeLayout
,我正在使用 LinearLayout
并且它完美地工作。 - drakeet如果小部件未出现在操作栏中,可能是因为您正在为操作栏使用appCompat。要解决此问题,请在menu.xml中的“showAsAction”和“actionLayout”前面将“android:”切换为“app:”
在xml中添加项目,使用app:替换android:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/myswitch"
android:title=""
app:showAsAction="always"
app:actionLayout="@layout/switch_layout"
/>
</menu>
制作用于您的“app:actionLayout”的布局
switch_layout
<?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/switchAB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
/>
</RelativeLayout>
像平常一样在您的ActionBarActivity中展开菜单
getMenuInflater().inflate(R.menu.mainmenu, menu);
return true;
如果开关没有显示出来,这应该会在您的操作栏中显示开关。
想要添加的人,
Kotlin
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
menuInflater.inflate(R.menu.menu_main, menu)
val item = menu!!.findItem(R.id.my_switch_item)
item.setActionView(R.layout.switch_layout)
val mySwitch = item.actionView.findViewById(R.id.switch_id)
mySwitch.setOnCheckedChangeListener(object : CompoundButton.OnCheckedChangeListener{
override fun onCheckedChanged(p0: CompoundButton?, isChecked: Boolean) {
// do what you want with isChecked
}
})
return true
}
Java
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
MenuItem item = menu.findItem(R.id.my_switch_item);
item.setActionView(R.layout.switch_layout);
Switch mySwitch = item.getActionView().findViewById(R.id.switch_id);
mySwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// do something based on isChecked
}
});
return true;
}
附注:您可以将引用更改为Switch或SwitchCompat。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" >
<Switch
android:id="@+id/actionbar_switch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
</RelativeLayout>
ActionBar actionBar = getSupportActionBar();
actionBar.setCustomView(R.layout.actionbar_top);
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_SHOW_CUSTOM);
...
Switch button = (Switch) findViewById(R.id.actionbar_switch);
Switch
与标题具有相同的高度,您应该在Switch
中添加android:layout_centerVertical="true"
。 - Hải PhongactionLayout
和showAsAction
请使用app
命名空间。 - Harsh VakhariaonOptionsItemSelected
方法中处理其点击事件,请问该怎么做? - Firas Al Mannaa