Android-将开关添加到操作栏不起作用

Android-将开关添加到操作栏不起作用,android,android-actionbar,switch-statement,Android,Android Actionbar,Switch Statement,我按照下面链接波纹管上说明的程序操作,但由于某些原因,开关从未显示。我按照其他线程中的建议更改了可见性以匹配我的应用程序名称空间,但没有效果。如果我尝试添加图标或常规按钮,该过程可以正常工作 非常感谢 我在Nexus4和Android 4.4.4上运行它 以下是我当前的xml内容 /menu/main.xml <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://sch

我按照下面链接波纹管上说明的程序操作,但由于某些原因,开关从未显示。我按照其他线程中的建议更改了可见性以匹配我的应用程序名称空间,但没有效果。如果我尝试添加图标或常规按钮,该过程可以正常工作

非常感谢

我在Nexus4和Android 4.4.4上运行它

以下是我当前的xml内容

/menu/main.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="com.example.testapp.MainActivity" >

    <item
        android:id="@+id/myswitch"
        android:title=""
        app:showAsAction="always"
        android:actionLayout="@layout/switch_layout"
    />  
</menu>

/layout/switch_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
     > <!-- android:layout_height="50dp" -->

    <Switch
        android:id="@+id/switchForActionBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        />
</RelativeLayout>

好的,我终于找到了适合我的东西

我创建了一个布局文件:/layout/text\u layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
     > <!-- android:layout_height="50dp" -->

    <Switch
        android:id="@+id/switchForActionBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        />
</RelativeLayout>
博姆,给你

令人印象深刻的是,在Android上,简单的事情有时会变得如此复杂


谢谢您的建议。

您是否尝试过使用
相对布局来代替
线性布局
?这里有一种不同的方法将开关添加到@Sash\u KP中-是。首先,我从链接中尝试了RelativeLayout。@Sagar-我也尝试过,菜单上只显示“服务”。动作栏布局本身可能有问题吗?(假设这是有道理的)。