我想要一个标题栏,显示各种图标,点击后会将用户引导到相应的活动页面。不幸的是,我遇到了以下问题: - 隐藏活动标题 -Icons未显示在活动栏上,但仅显示为下拉列表
以下是我在android manifest中使用的代码
<activity
android:name="com.dooba.beta.MoodActivity"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.Holo">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
下面是菜单xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@+id/pageExperience"
android:icon="@drawable/fork_spoon3"
android:title="@string/action_experience"/>
<item android:id="@+id/pageMessaging"
android:icon="@drawable/messageicon"
android:title="@string/action_messaging"/>
<item android:id="@+id/pageEventsBooking"
android:icon="@drawable/fork_spoon3"
android:title="@string/action_book"/>
<item android:id="@+id/pageProfile"
android:icon="@drawable/fork_spoon3"
android:title="@string/action_profile" />
<item android:id="@+id/pageReport"
android:icon="@drawable/fork_spoon3"
android:title="@string/action_report" />
<item android:id="@+id/pageAbout"
android:icon="@drawable/fork_spoon3"
android:title="@string/action_about" />
</menu>
以下是调用菜单的活动代码
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main_activity_actions, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.pageExperience:
openPageExperience();
return true;
case R.id.pageMessaging:
openPageMessage();
return true;
case R.id.pageEventsBooking:
openPageBook();
return true;
case R.id.pageProfile:
openPageProfile();
return true;
case R.id.pageReport:
openPageReport();
return true;
case R.id.pageAbout:
openPageAbout();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void openPageAbout() {
// TODO Auto-generated method stub
}
private void openPageReport() {
// TODO Auto-generated method stub
}
private void openPageProfile() {
// TODO Auto-generated method stub
}
private void openPageBook() {
// TODO Auto-generated method stub
}
private void openPageMessage() {
// TODO Auto-generated method stub
}
private void openPageExperience() {
// TODO Auto-generated method stub
}
}
下面是引用标题图标字符串名称
的字符串xml文件 <string name="action_experience">Experience</string>
<string name="action_messaging">Messaging</string>
<string name="action_book">Book</string>
<string name="action_profile">Profile</string>
<string name="action_report">"Report</string>
<string name="short_name"></string>
<string name="action_about">About</string>
更新 尝试实现代码时,我收到了以下问题 ActionBar ab = getActionBar(); getActionBar 类型不匹配:无法从android.app.ActionBar转换为android.support.v7.app.ActionBar
customActionBarView无法解析为变量 main_activity_actions无法解析或不是字段
活动代码
public class MoodActivity extends Activity {
@Override
public void onResume() {
super.onResume(); // Always call the superclass method first
setCustomActionBarView();
}
private void setCustomActionBarView() {
// TODO Auto-generated method stub
ActionBar ab = getActionBar();
ab.setDisplayHomeAsUpEnabled(false);
ab.setDisplayShowTitleEnabled(false);
ab.setDisplayShowCustomEnabled(true);
if(customActionBarView == null){
LayoutInflater inflator = (LayoutInflater) this .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
customActionBarView = inflator.inflate(R.layout.main_activity_actions,null);
}
ab.setCustomView(customActionBarView);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mood);
final TextView teating = (TextView) this.findViewById(R.id.tdinning);
teating.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
MoodActivity.this.startActivity(new Intent(MoodActivity.this, CasualEventsActivity.class));
}
});
final ImageView ieating = (ImageView) this.findViewById(R.id.idinning);
ieating.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
MoodActivity.this.startActivity(new Intent(MoodActivity.this, CasualEventsActivity.class));
}
});
final TextView tdrinks = (TextView) this.findViewById(R.id.tcasual);
tdrinks.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
MoodActivity.this.startActivity(new Intent(MoodActivity.this, CasualEventsActivity.class));
}
});
final ImageView idrinks = (ImageView) this.findViewById(R.id.icasual);
idrinks.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
MoodActivity.this.startActivity(new Intent(MoodActivity.this, CasualEventsActivity.class));
}
});
final TextView tshows = (TextView) this.findViewById(R.id.tshows);
tshows.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
MoodActivity.this.startActivity(new Intent(MoodActivity.this, CultureEventsActivity.class));
}
});
final ImageView ishows = (ImageView) this.findViewById(R.id.ishows);
ishows.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
MoodActivity.this.startActivity(new Intent(MoodActivity.this, CultureEventsActivity.class));
}
});
final TextView tarts = (TextView) this.findViewById(R.id.tculture);
tarts.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
MoodActivity.this.startActivity(new Intent(MoodActivity.this, CultureEventsActivity.class));
}
});
final ImageView iarts = (ImageView) this.findViewById(R.id.iculture);
iarts.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
MoodActivity.this.startActivity(new Intent(MoodActivity.this, CultureEventsActivity.class));
}
});
final Button viewall = (Button) this.findViewById(R.id.brandom);
viewall.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
MoodActivity.this.startActivity(new Intent(MoodActivity.this, CasualEventsActivity.class));
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main_activity_actions, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.pageExperience:
openPageExperience();
return true;
case R.id.pageMessaging:
openPageMessage();
return true;
case R.id.pageEventsBooking:
openPageBook();
return true;
case R.id.pageProfile:
openPageProfile();
return true;
case R.id.pageReport:
openPageReport();
return true;
case R.id.pageAbout:
openPageAbout();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void openPageAbout() {
// TODO Auto-generated method stub
}
private void openPageReport() {
// TODO Auto-generated method stub
}
private void openPageProfile() {
// TODO Auto-generated method stub
}
private void openPageBook() {
// TODO Auto-generated method stub
}
private void openPageMessage() {
// TODO Auto-generated method stub
}
private void openPageExperience() {
// TODO Auto-generated method stub
}
}
更新 我现在有以下问题: getSupportActionBar(); 它给了我以下问题: 对于MoodActivity类型
,方法getSupportActionBar()未定义@Override
public void onResume() {
super.onResume(); // Always call the superclass method first
setCustomActionBarView();
}
private void setCustomActionBarView() {
// TODO Auto-generated method stub
ActionBar ab = getSupportActionBar();
ab.setDisplayHomeAsUpEnabled(false);
ab.setDisplayShowTitleEnabled(false);
ab.setDisplayShowCustomEnabled(true);
View customActionBarView;
if(customActionBarView == null){
LayoutInflater inflator = (LayoutInflater) this .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
customActionBarView = inflator.inflate(R.layout.main_activity_actions,null);
}
ab.setCustomView(customActionBarView);
}
答案 0 :(得分:2)
ActionBar ab = getActionBar();
ab.setDisplayHomeAsUpEnabled(false);
ab.setDisplayShowTitleEnabled(false);
ab.setDisplayShowCustomEnabled(true);
if(customActionBarView == null){
LayoutInflater inflator = (LayoutInflater) this .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
customActionBarView = inflator.inflate(R.layout.my_custom_view,null);
}
ab.setCustomView(customActionBarView);
这是它的作用:
setDisplayHomeAsUpEnabled(boolean showHomeAsUp)
当您使用导航抽屉并且希望用户在视图层次结构中上升级别时,将使用。
setDisplayShowTitleEnabled(boolean showTitle);
用于决定是否应显示活动的标题
setDisplayShowCustomEnabled(boolean showCustom);
用于决定是否要显示ActionBar
然后,您可以使用customActionBarView.findViewById()
检索图标并为其添加侦听器。