如何将开关添加到android操作栏?

如何将开关添加到android操作栏?,android,android-layout,Android,Android Layout,我想添加一个类似于jellybean原生外观的按钮开关。(视图顶部的蓝色/灰色开关) 文档显示了如何在那里创建菜单或添加图标,但没有说明如何添加自定义元素。开关。 为交换机创建布局switch\u layout.xml。菜单的自定义布局应始终为RelativeLayout <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/re

我想添加一个类似于jellybean原生外观的按钮开关。(视图顶部的蓝色/灰色开关)

文档显示了如何在那里创建菜单或添加图标,但没有说明如何添加自定义元素。开关。

为交换机创建布局
switch\u 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
充气

getMenuInflater().inflate(R.menu.mainmenu, menu);
return true;

以西结所给出的解决方案是令人敬畏的,而且有效。下面是另一种方法:

定义自定义布局:

<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>

终于解决了我的问题:对于那些使用新AppCompat的人,你应该使用
android.support.v7.widget.SwitchCompat
,而不是开关布局上的
Switch
。否则,它将不会显示在
操作栏上(假设你也使用AppCompat操作栏),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>

如果小部件没有出现在操作栏中,可能是因为您正在将appCompat用于操作栏。要解决此问题,请将“android:”切换到“app:”,位于menu.xml中的“showAsAction”和“actionLayout”前面

将项目添加到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>

这将使开关出现在您的操作栏中,如果它没有出现。

对于那些想要添加的人

选中将侦听器更改为同一开关 科特林

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
}
爪哇

另外,您可以将引用更改为Switch或SwitchCompat


嘿,对于“菜单的自定义布局应始终为RelativeLayout”。是否存在任何文档状态?您应该将
android:layout\u centerVertical=“true”
添加到
开关中,使其与标题具有相同的高度不要忘记添加“SethasOptions菜单(true);”对于片段,强制调用CreateOptions菜单。使用
app
命名空间来调用
actionLayout
showAsAction
如果开关不可见,我正在使用appcompat,我使用app命名空间来调用actionLayout和showAsAction,但我无法处理其单击
OnOptions ItemSelected
方法?怎么做?哇。。。这解决了我的问题!!!我似乎无法使用以西结的解决方案,但这是我的解决方案!也许这跟我现在用的新工具栏有关吧???无论如何,谢谢你!注意:这似乎不同于我最初希望将其作为菜单的自定义视图的方法。。。但是有没有办法让这个自定义视图与操作栏的右侧对齐?为那些同舟共济的人找出并分享答案。@VuNguyen太棒了!我仍然在处理它,因为我在一个片段中有这个ActionBar,findViewById返回null,解决后将更新:D@arthursfreire你好我不确定你的代码是什么样子的,但是给它一个创建方法的峰值,特别是在@arthursfreire中的第80行之后,这是我自己编写的全功能代码,我仍在进行该项目,支持SDK 16到21。这是对上述答案的微妙但极好的简化/补充!像这样的小细节很容易监督,却忘记了调整。我总是花上几个小时试图找出错误。现在,如果菜单中没有显示某些内容,我有一个经验法则来检查名称空间。我知道这已经很长时间了,但这个答案对我很有效,现在的问题是如何获得开关,以便以编程方式将侦听器连接到它?使用
SwitchCompat
还有另一个优点:开关以更近的外观显示(例如材料设计)。为什么每个人都在RelativeLayout上使用android:orientation=“horizontal”
?@zackygaurav作为我的测试,它不需要使用RelativeLayout
,我使用的是
LinearLayout
,它工作得非常完美。
@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;
}
<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>
<?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>
   getMenuInflater().inflate(R.menu.mainmenu, menu);
    return true;
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
}
@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;
}