删除操作栏图标自定义视图

删除操作栏图标自定义视图

本文介绍了删除操作栏图标自定义视图,并显示家庭向上按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以添加自定义视图动作条。我的应用程序从版本2.3,所以我用了 ActionbarActivity 支持。现在,我的查询是从动作条删除应用程序图标,只有有家庭了图标即回图像。

I am able to add custom view in actionbar. My application is supporting from version 2.3 so I had used ActionbarActivity. Now my query is to remove app icon from the actionbar and only to have home up icon i.e back image.

我试过很多的选择,我发现,当搜寻。但它不工作与自定义视图。

I had tried lots of option that I found while searching. But its not working with custom view.

ActionBar actionBar = getSupportActionBar();
    LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT,
            LayoutParams.WRAP_CONTENT, Gravity.RIGHT
                    | Gravity.CENTER_VERTICAL);
    View customNav = LayoutInflater.from(this).inflate(
            R.layout.custom_action_view, null); // layout which contains

    actionBar.setCustomView(customNav, lp);


    actionBar.setDisplayHomeAsUpEnabled(true);
    actionBar.setDisplayShowHomeEnabled(true);
    actionBar.setDisplayShowCustomEnabled(true);

    actionBar.setBackgroundDrawable(new ColorDrawable(Color
            .parseColor("#a40404")));

帮我出这个问题。先谢谢了。

Help me out with this issue. Thanks in advance.

推荐答案

向上按钮连接到家庭图标。这就是为什么它被称为回家了 setDisplayHomeAsUpEnabled(真)

The up button is tied to home icon. That's why it's called "home as up" setDisplayHomeAsUpEnabled(true).

所以,你不能表现出向上的箭头与残疾人的家的图标。

So you can't show up arrow with disabled "home" icon.

但你可以做一个视图,看起来像它在你的自定义视图。

But you can make a View that looks like it in your custom View.

例如:

    <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:src="?attr/homeAsUpIndicator"

将根据您的主题设置的向上箭头。

Will set the up arrow based on your theme.

android:background="?attr/actionBarItemBackground"

将设置蓝色选择背景pressed状态

Will set the blue selection background for pressed state

现在设置一个OnClickListener带箭头的布局

Now set an OnClickListener to a layout with arrow

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

这篇关于删除操作栏图标自定义视图,并显示家庭向上按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 17:21