2015-01-04 2 views
2

Я хочу добавить тумблер в свою панель действий, однако, похоже, он не появляется. Это то, что я получаю, как мой экран https://drive.google.com/file/d/0B1o-MgL-CQBFbml4VWZRdlptUWM/view?usp=sharingВиджет костюма (Switch) Не отображается в 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 часа, пытаясь понять, что я делаю неправильно. –

+0

да, это вызвано использованием 'appcompat' – mmlooloo

Смежные вопросы