Remove Action bar Icon with custom view and show home up button

霸气de小男生 提交于 2020-01-04 14:36:34

问题


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.

edit

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.


回答1:


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.

For example:

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

Where

android:src="?attr/homeAsUpIndicator"

Will set the up arrow based on your theme.

android:background="?attr/actionBarItemBackground"

Will set the blue selection background for pressed state

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



回答2:


To remove Actonbar Icon:

Just add actionBar.setDisplayUseLogoEnabled(false); this line in oncreate() method:

To enable back button:

you need to extends SherlockActivity and insert this code in oncreate() method:

getSupportActionBar().setDisplayHomeAsUpEnabled(true);

Handle back button action using this code:

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


来源:https://stackoverflow.com/questions/22194107/remove-action-bar-icon-with-custom-view-and-show-home-up-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!