[英]ActionBar showAsAction not working
After a lot of googling, I have not been able to solve this issue. 经过大量的搜寻后,我无法解决此问题。 I am trying to run a very simple code that just creates an ActionBar, puts a button on it and adds a listener to that button.
我正在尝试运行一个非常简单的代码,该代码仅创建一个ActionBar,在其上放置一个按钮,然后向该按钮添加一个侦听器。 The problem is that the first item does not appear as an Action but instead appears with the dropdown with Settings even though I have set it showAsAction="always".
问题是,即使我将其设置为showAsAction =“ always”,第一个项目也不会显示为动作,而是与带有设置的下拉列表一起出现。 This is my xml
这是我的xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".MainActivity">
<item android:id="@+id/action_create_order"
android:title="@string/action_create_order"
android:icon="@drawable/ic_web_asset_black_24dp"
android:orderInCategory="1"
app:showAsAction="always"></item>
<item android:id="@+id/action_settings"
android:title="@string/action_settings"
android:orderInCategory="100"
app:showAsAction="never" />
</menu>
This is my MainActivity.java 这是我的MainActivity.java
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.action_create_order:
Intent intent=new Intent(this, OrderActivity.class);
startActivity(intent);
return true;
case R.id.action_settings:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
Any help is appreciated. 任何帮助表示赞赏。
app:showAsAction
is used with AppCompatActivity . app:showAsAction
与AppCompatActivity app:showAsAction
使用。 You must either extend that class and use AppCompat
or use android:showAsAction
. 您必须扩展该类并使用
AppCompat
或使用android:showAsAction
。
use android:showAsAction="always
使用
android:showAsAction="always
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".MainActivity">
<item android:id="@+id/action_create_order"
android:title="@string/action_create_order"
android:icon="@drawable/ic_web_asset_black_24dp"
android:orderInCategory="1"
android:showAsAction="always"> </item>
<item android:id="@+id/action_settings"
android:title="@string/action_settings"
android:orderInCategory="100"
android:showAsAction="never" />
</menu>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.