我搜索了论坛,发现使用app:showAsAction是针对我的问题的建议。但是,我正在使用app:showAsAction,仍然无法在操作栏上显示我的图标。另外,我还必须在Main.java中手动设置操作栏标题,因为使用xml时,标题不会出现在操作栏上。问题:如何使图标显示在操作栏上,为什么不能在xml中设置操作栏标题?这些问题相关吗?这是我的代码:
主要XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#000000"
tools:context="example.com.listview.MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<android.support.v7.widget.Toolbar
android:id="@+id/myToolbar"
android:layout_width="match_parent"
android:layout_height="56dp"
android:background="#8380"
android:elevation="4dp"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
android:titleTextColor="@android:color/holo_green_light"
app:titleTextColor="@android:color/holo_green_light"
/>
</RelativeLayout>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/my_recycler_view"/>
</LinearLayout>
主要的JAVA:
package example.com.listview;
import android.app.Activity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuInflater;
public class MainActivity extends Activity {
private RecyclerView recyclerView;
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
Repository myDataset;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar myToolbar = (Toolbar) findViewById(R.id.myToolbar);
myToolbar.setTitle("ListView");
myDataset = new Repository();
myDataset.addCause("Happy");
myDataset.addCause("Sad");
myDataset.addCause("Love");
myDataset.causes.get(0).addOccurrence();
myDataset.causes.get(0).addOccurrence();
myDataset.causes.get(1).addOccurrence();
myDataset.causes.get(2).addOccurrence();
recyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
recyclerView.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(mLayoutManager);
mAdapter = new MyAdapter(myDataset);
recyclerView.setAdapter(mAdapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu){
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity_main_actions, menu);
return super.onCreateOptionsMenu(menu);
}
}
主菜单动作:
<?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/addCause"
android:title="Add Cause"
android:orderInCategory="0"
android:icon="@drawable/plussign"
app:showAsAction="never"/>
<item android:id="@+id/removeCause"
android:orderInCategory="1"
android:title="Remove Cause"
app:showAsAction="never"
android:icon="@drawable/minussign"/>
</menu>
表现:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="example.com.listview">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/Theme.AppCompat.Light.NoActionBar">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
最佳答案
如何让我的图标显示在操作栏上
AppCompatActivity
扩展,而不是Activity
setSupportActionBar(myToolbar);
为什么我不能在xml中设置动作栏标题
在XML中添加
app:title="My Title"
应该可以。