gpt4 book ai didi

java - 隐藏操作栏中的标题并显示图标

转载 作者:行者123 更新时间:2023-12-02 05:33:03 27 4
gpt4 key购买 nike

我试图有一个标题栏,显示各种图标,单击这些图标将引导用户进入适当的 Activity 页面。不幸的是,我遇到了以下问题:-隐藏 Activity 标题-图标不显示在 Activity 栏上,仅显示为下拉列表

下面是我在 android list 中使用的代码

 <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>

下面是调用菜单的 Activity 代码

@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();获取操作栏类型不匹配:无法从 android.app.ActionBar 转换为 android.support.v7.app.ActionBar

customActionBarView 无法解析为变量main_activity_actions 无法解析或不是字段

Activity 代码

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);
}

最佳答案

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)  

当您使用抽屉式导航栏并且希望用户在 View 层次结构中向上移动时使用。

setDisplayShowTitleEnabled(boolean showTitle);  

用于决定是否显示 Activity 的标题

setDisplayShowCustomEnabled(boolean showCustom);  

用于决定是否要为ActionBar显示自定义 View

然后,您可以使用 customActionBarView.findViewById() 检索图标并向其添加监听器。

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

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