【问题标题】:Cant use Appbar with Buttons无法使用带有按钮的 Appbar
【发布时间】:2015-05-18 01:58:59
【问题描述】:

我开始使用 Holo 主题,我想用这个主题构建我的应用程序(不使用 Compact)

问题是我的应用栏没有按钮。 (我不能使用 ifRoom 选项来显示菜单图标)。

我的菜单代码:

<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" />

还有我的 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);
    }
}

谢谢

【问题讨论】:

  • 您为什么使用 Holo 主题而不是 AppCompat?您的应用将在

标签: java android appbar


【解决方案1】:

app:showAsAction 仅在您使用 AppCompat 时适用,强烈建议您在所有 API 级别之间保持兼容性和一致性。

如果您只想使用系统提供的操作栏,则应将所有app:showAsAction 实例替换为android:showAsAction

【讨论】:

  • 谢谢你!我觉得自己好傻XD。起初我遇到了一个错误,但它仍然有效!再次感谢
  • 如果这有助于回答您的问题,请确保您接受答案以将其从未回答队列中删除。很高兴你成功了!
猜你喜欢
  • 2022-11-14
  • 1970-01-01
  • 2018-05-01
  • 2015-09-26
  • 2019-09-04
  • 1970-01-01
  • 2017-12-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多