Android的自定义操作栏不填父母,标题不居中对齐 [英] 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>
这是由一个GraphicsBuilder类以下述方式充胀:
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?
推荐答案
我知道这是很长一段时间了。 通过改变布局PARAMS一旦它被设置为自定义视图解决了这个问题。
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);
它已经得到了它的尺寸preSET。 我的解决办法是:
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
这篇关于Android的自定义操作栏不填父母,标题不居中对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!