[英]Hide title in action bar and display 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(); 它给了我以下问题:方法 getSupportActionBar() 未定义为 MoodActivity 类型
@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);
}
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()
来检索您的图标并向它们添加侦听器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.