简体   繁体   中英

Android Custom Action bar not filling parent, title not center aligned

I have been experimenting the following issue: I have created a custom action bar:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/com_facebook_blue" >

<TextView 
    android:id="@+id/action_bar_title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:maxLines="1"
    android:layout_marginLeft="@dimen/activity_horizontal_margin"
    android:layout_centerInParent="true"
    android:textSize="30dp"
    android:textColor="#ffffff"
    android:text="@string/app_title"/>

</RelativeLayout>

Which is inflated by a GraphicsBuilder class in the following way:

public static void setupActionBar(String title, Activity a) {

        final ViewGroup actionBarLayout = (ViewGroup) a.getLayoutInflater()
                .inflate(R.layout.action_bar_layout, null);
        final ActionBar actionBar = a.getActionBar();

        actionBar.setDisplayShowHomeEnabled(false);
        actionBar.setDisplayShowTitleEnabled(false);
        actionBar.setDisplayShowCustomEnabled(true);
        actionBar.setCustomView(actionBarLayout);
        final int actionBarColor = a.getResources().getColor(R.color.main_red);
        actionBar.setBackgroundDrawable(new ColorDrawable(actionBarColor));

        TextView tv = (TextView) actionBarLayout.findViewById(R.id.action_bar_title);
        Typeface font = Typeface.createFromAsset(a.getAssets(), "fonts/Molle-Regular.ttf");
        tv.setText(title);
        tv.setTypeface(font);
    }

Which leads to the following results (I have kept the title bar background blue to highlight the issue):

在此输入图像描述

Basically it looks like that the custom action bar does not fill the parent width, therefore any try to align the title at the center is useless. How can I fix this?

I know this is a long time gone. Solved it by changing the layout params once it's been set as custom view.

My logic was that when you inflate it the first time, it assumes the size of the original container. When you call:

actionBar.setCustomView(titlebar);

It's already got its dimensions preset. My solution was:

View titlebar = inflater.inflate(R.layout.titlebar, null);

// Do stuff to Title Bar //

ActionBar actionBar = getActionBar();
actionBar.setCustomView(titlebar);
View v = actionBar.getCustomView();
LayoutParams lp = v.getLayoutParams();
lp.width = LayoutParams.MATCH_PARENT;
v.setLayoutParams(lp);

Alternatively, you can first set the action bar layout without the inflater:

ActionBar actionBar = getActionBar();
actionBar.setCustomView(R.id.titlebar);
View v = actionBar.getCustomView();
// Do something to the view  E.g Set button listeners blah //

// Rest of code

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