Android 删除带有自定义视图和“显示主页”按钮的操作栏图标
我可以在Android 删除带有自定义视图和“显示主页”按钮的操作栏图标,android,android-actionbar,android-custom-view,Android,Android Actionbar,Android Custom View,我可以在actionbar中添加自定义视图。我的应用程序支持2.3版,因此我使用了ActionbarActivity。现在我的查询是从操作栏中删除应用程序图标,只有主页图标,即返回图像 我在搜索时尝试了很多选择。但它不适用于自定义视图 编辑 帮我解决这个问题。提前感谢。要删除Actonbar图标: 只需添加actionBar.setDisplayUseLogoEnabled(false)此行位于oncreate()方法中: getSupportActionBar().setDisplayHome
actionbar
中添加自定义视图。我的应用程序支持2.3版,因此我使用了ActionbarActivity
。现在我的查询是从操作栏中删除应用程序图标
,只有主页图标,即返回图像
我在搜索时尝试了很多选择。但它不适用于自定义视图
编辑
帮我解决这个问题。提前感谢。要删除Actonbar图标: 只需添加
actionBar.setDisplayUseLogoEnabled(false)代码>此行位于oncreate()
方法中:
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
要启用后退按钮:
您需要扩展SherlockActivity并将此代码插入oncreate()
方法:
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
使用以下代码处理后退按钮操作:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// do your action here when click on back button showing on action bar
onBackPressed();
}
}
向上按钮与主图标相连。这就是为什么它被称为“home as up”setDisplayHomeAsUpEnabled(true)
因此,您不能用禁用的“主页”图标显示向上箭头
但是您可以创建一个在自定义视图中看起来像它的视图
例如:
<LinearLayout
android:id="@android:id/home"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal"
android:background="?attr/actionBarItemBackground"
android:paddingRight="5dp">
<!-- this will show tha up arrow -->
<ImageView
android:id="@+id/up"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:src="?attr/homeAsUpIndicator"/>
<!-- place here any View that will be clickable along with "up" arrow (in this example, it's an icon) -->
<ImageView
android:id="@+id/home_icon"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginTop="@dimen/action_bar_icon_vertical_padding"
android:layout_marginBottom="@dimen/action_bar_icon_vertical_padding"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
android:src="@drawable/app_icon"/>
</LinearLayout>
将根据您的主题设置向上箭头
android:background="?attr/actionBarItemBackground"
将为按下状态设置蓝色选择背景
现在将OnClickListener设置为带有箭头的布局
View customNav = LayoutInflater.from(this).inflate(
R.layout.custom_action_view, null);
customNav.findViewById(android.R.id.home).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick() {
// up button clicked
}
});
actionBar.setCustomView(customNav, lp);
如果没有主页图标,则无法显示按钮。@doctordrive不可能吗?
View customNav = LayoutInflater.from(this).inflate(
R.layout.custom_action_view, null);
customNav.findViewById(android.R.id.home).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick() {
// up button clicked
}
});
actionBar.setCustomView(customNav, lp);