简体   繁体   中英

Android custom action bar remove icon

Below is a screenshot of my current setup.

在此处输入图片说明

I have created a custom action bar view, which I set in the code below, and what I would like is two images, one left aligned in the title bar, the other right aligned.

The problem is, that when I hide the app icon, it only hides it, not removes it hence the gap on the left. I found a couple of other SO questions that show how to remove the icon, but that also removes the tabs which I want to keep.

Can any one offer me a solution?

From my onCreate() function:

final ActionBar actionBar = getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);    

        LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = inflater.inflate(R.layout.action_bar_title, null);

        View homeIcon = findViewById(android.R.id.home);
        ((View) homeIcon.getParent()).setVisibility(View.GONE);
        ((View) homeIcon).setVisibility(View.GONE);        

        actionBar.setDisplayShowCustomEnabled(true);
        actionBar.setDisplayShowTitleEnabled(false);  
        actionBar.setDisplayShowHomeEnabled(true);
        actionBar.setCustomView(v);

My xml custom layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"    
    android:layout_height="fill_parent"
    android:gravity="fill_horizontal"
    android:layout_marginLeft="0dp"
    android:orientation="horizontal"
    >    

    <ImageView android:id="@+id/title_img_left"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentTop="true"                    
                    android:src="@drawable/test" />
     <ImageView android:id="@+id/title_img_right"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"
                    android:layout_alignParentTop="true"
                    android:src="@drawable/test" />


</RelativeLayout>

I'm using the Holo.Light.DarkActionBar theme I believe.

So if I understand the thing which you are trying to achieve, why you just don't add a MenuItem with your custom image on left side and set you actionBarLogo with the image which you want to be on the right side. After that just override home button click and I guess you will achieve the same thing which you can create with custom view, without the padding on the left side of ActionBar .

PS If I understand you wrong, please give some more information on how it should look like.

the code is working!!!

ActionBar actionBar = getSherlockActivity().getSupportActionBar();

        View customNav = LayoutInflater.from(getActivity()).inflate(R.layout.custom_action_bar_view, null);
        ImageButton imageButton = (ImageButton) customNav.findViewById(R.id.imageButton);

        ActionBar.LayoutParams lp = new ActionBar
            .LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
        actionBar.setCustomView(customNav, lp);
        actionBar.setDisplayShowCustomEnabled(true);
        actionBar.setDisplayShowTitleEnabled(false);
        actionBar.setDisplayShowHomeEnabled(false);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM