简体   繁体   English

无法通过按钮使用Appbar

[英]Cant use Appbar with Buttons

I started working with the Holo Theme, and I want to build my app with this Theme (without using the Compact) 我开始使用Holo主题,并且想要使用该主题构建我的应用程序(不使用Compact)

The problem is that my App bar is whithout the Buttons. 问题是我的应用程序栏没有按钮。 (I cannot use the ifRoom option for showing the menus icons). (我不能使用ifRoom选项显示菜单图标)。

my menu code: 我的菜单代码:

<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=".MainActivity">
<item
    android:id="@+id/action_settings"
    android:orderInCategory="100"
    android:title="@string/action_settings"
    app:showAsAction="never" />
<item
    android:id="@+id/action_search"
    android:icon="@drawable/ic_action_search"
    android:orderInCategory="105"
    app:showAsAction="ifRoom"
    android:title="@string/action_search"
    />
<item
    android:id="@+id/action_edit"
    android:icon="@drawable/ic_action_edit"
    android:orderInCategory="110"
    android:title="@string/action_edit"
    app:showAsAction="ifRoom" />

and my Java Class: 和我的Java类:

package myfirsttry.another;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;


public class MainActivity extends Activity{

    public final static String EXTRA_MESSAGE = "linir.key";

    @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);
    }
    public void sendMessage(View view){
        Intent intent = new Intent(this, secondActivity.class);
        EditText editText = (EditText)findViewById(R.id.etInput);
        String message = editText.getText().toString();
        intent.putExtra(EXTRA_MESSAGE,message);
        startActivity(intent);
    }
}

Thanks 谢谢

The app:showAsAction is only applicable if you use AppCompat as is strongly recommended for compatibility and consistency across all API levels. 只有强烈建议您使用AppCompat时app:showAsAction才适用,强烈建议您在所有API级别上都具有兼容性和一致性。

If you'd like to only use the system provided action bar, then you'd replace all instances of app:showAsAction with android:showAsAction . 如果您只想使用系统提供的操作栏,则可以将app:showAsAction所有实例替换为android:showAsAction

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM