55

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

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

4

5 回答 5

98

为交换机创建布局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;
于 2013-08-11T14:04:34.167 回答
36

终于找到了我的问题:对于那些使用新 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>

然后在代码中设置布局:

@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;
}
于 2014-12-05T20:11:53.840 回答
33

如果小部件未出现在操作栏中,则可能是因为您将 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;

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

于 2015-01-04T16:06:34.873 回答
12

对于那些想要添加的人,

选中将侦听器更改为同一交换机

科特林

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

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

于 2018-09-17T12:54:18.570 回答
5

Ezequiel 提供的解决方案非常棒且有效。这是另一种方法:

定义您的自定义布局:

<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);
于 2014-12-05T19:13:40.557 回答