gpt4 book ai didi

java - 操作项图标未显示在操作栏中

转载 作者:行者123 更新时间:2023-12-01 12:31:47 25 4
gpt4 key购买 nike

我正在尝试将操作项添加到操作栏,但操作项的图标未显示。XML 文件 res/menu/actionbar:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >

<item android:id="@+id/action_search"
android:icon="@drawable/ic_action_search"
android:title="test"
android:showAsAction="always" />

<item android:id="@+id/action_settings"
android:title="@string/action_settings"
android:showAsAction="never" />"
</menu>

添加操作栏

import android.support.v7.app.ActionBarActivity;

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.
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.actionbar, menu);
return super.onCreateOptionsMenu(menu);
}

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

switch(id)
{
case R.id.action_search:
Context context = getApplicationContext();
CharSequence text = "Action Button Pressed!";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
return true;
default:
return super.onOptionsItemSelected(item);

}

}



}

我将 list 文件中的最小sdk设置为7

<uses-sdk android:minSdkVersion="7" />

最佳答案

在你的菜单 xml 中尝试这个

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" >

<item android:id="@+id/action_search"
android:icon="@drawable/ic_action_search"
android:title="test"
app:showAsAction="always" />

<item android:id="@+id/action_settings"
android:title="@string/action_settings"
app:showAsAction="never" />"
</menu>

关于java - 操作项图标未显示在操作栏中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25873180/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com