如何向android操作栏添加开关?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12107031/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:44:54  来源:igfitidea点击:

How to add a switch to android action bar?

androidandroid-layout

提问by Arturs Vancans

I would like to add a button switch similar to jellybean native look. (Blue/gray switch at the top of the view) enter image description here

我想添加一个类似于 jellybean 原生外观的按钮开关。(视图顶部的蓝色/灰色开关) 在此处输入图片说明

Documentation shows how to create a menu there or add icons, but it does not say, how to add a custom elements. eg. a switch. http://developer.android.com/guide/topics/ui/actionbar.html

文档显示了如何在那里创建菜单或添加图标,但没有说明如何添加自定义元素。例如。一个开关。 http://developer.android.com/guide/topics/ui/actionbar.html

回答by Ezequiel

Create a layout for the switch switch_layout.xml. Custom layouts for menu should always be RelativeLayout

为 switch 创建一个布局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>

Then, in your mainmenu.xmladd the item as follows

然后,在您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>

And in your activity, inflate the mainmenu.xmlas you always do

在你的活动中,mainmenu.xml像往常一样充气

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

回答by Vu Nguyen

Finally figured out my problem: for those that's using the new AppCompat, you should be using android.support.v7.widget.SwitchCompatinstead of Switchon the switch layout...otherwise, it won't show on the ActionBar(assumed you're using AppCompat ActionBar as well), well, the actionLayout attribute doesn't work, it has to be set in the code.

终于解决了我的问题:对于那些使用新 AppCompat 的人,你应该使用android.support.v7.widget.SwitchCompat而不是Switch在 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>

Then set the layout in the code:

然后在代码中设置布局:

@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;
}

回答by Neil M.

If the widget is not appearing in the action-bar it is probably because you are using appCompat for your action-bar. To solve this switch "android:" to "app:"in front of "showAsAction" and "actionLayout" in your menu.xml

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

Add item to xml, with app: in place of android:

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

Make layout that you are using for your "app:actionLayout"
switch_layout

制作您用于“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>

Inflate the menu in your ActionBarActivity as you would normally

像往常一样膨胀 ActionBarActivity 中的菜单

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

This should make the switch appear in your action-bar, if it was not appearing.

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

回答by gprathour

For those who want to add,

想要补充的朋友们,

Checked change Listener to the same Switch

检查将侦听器更改为同一交换机

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

P.S. You can change reference to Switch or SwitchCompat

PS 您可以更改对 Switch 或 SwitchCompat 的引用

回答by Fabricio PH

The solution given by Ezequiel is awesome and works. Here goes another approach:

Ezequiel 给出的解决方案很棒并且有效。这是另一种方法:

Define your custom layout:

定义您的自定义布局:

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

Inflate it programatically:

以编程方式对其进行充气:

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