2015-01-04 46 views
2

我想添加一個切換開關到我的操作欄,但它似乎並沒有出現。 這是我得到我的屏幕 https://drive.google.com/file/d/0B1o-MgL-CQBFbml4VWZRdlptUWM/view?usp=sharing服裝部件(開關)沒有出現在ActionBar

我跟着fourms的另一篇文章來獲取代碼 - How to add a switch to android action bar? 嘗試了所有的建議的方法,但現在使用的埃塞基耶爾的

這是我的代碼

menu_main

<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.json.example.myapplication.MainActivity" > 
    <item 
     android:id="@+id/switchId" 
     android:title="" 
     app:showAsAction="always" 
     android:actionLayout="@layout/switch_layout"/> 
    <item 
     android:id="@+id/action_settings" 
     android:orderInCategory="100" 
     android:title="@string/action_settings" 
     app:showAsAction="never"/> 
</menu> 

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> 

的活動,充氣菜單

package com.json.example.myapplication; 

import android.support.v7.app.ActionBarActivity; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 


public class MainActivity extends ActionBarActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
    } 


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

     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 

     //noinspection SimplifiableIfStatement 
     if (id == R.id.action_settings) { 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 
} 

爲什麼開關沒有出現,我,我只是打錯東西(若然)或有錯誤,我讀指南?任何幫助是極大的讚賞。

回答

2

變化

android:actionLayout="@layout/switch_layout" 

app:actionLayout="@layout/switch_layout" 
+0

好工作的。僅僅因爲我使用的是appcompat,我遇到了這個問題?同樣非常感謝我花了3個小時試圖弄清楚我做錯了什麼。 – 2015-01-04 13:08:09

+0

是的,這是使用'appcompat'造成的 – mmlooloo 2015-01-04 13:10:42