问题描述
我在设备遇到一个问题,下面的Android 4.0,在ActionBarSherlock按钮有时只是不能垂直居中。这发生在运行机器人2.3.6设备,也不会在运行4.0任何设备影响+
I met a problem in device below Android 4.0, the button in the ActionBarSherlock sometimes just can't vertically centered. This happened on devices running Android 2.3.6, and will not affect on any device running 4.0+
我做了pretty的轻松自定义布局上ActionBarSherlock,其自定义布局结构显示在下面的XML
I made a pretty easy custom layout on the ActionBarSherlock, whose custom layout structure is shown in the XML below
<Button
android:id="@+id/button_action_next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="@color/people_app_theme_color"
android:gravity="center"
android:text="NEXT"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="@android:color/white" />
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:gravity="center"
android:orientation="horizontal" >
<ImageButton
android:id="@+id/button_action_home"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@color/people_app_theme_color"
android:gravity="center"
android:maxHeight="30dp"
android:src="@drawable/android_home"
android:text="HOME"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="@android:color/white" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:minHeight="48dp"
android:text="TextView"
android:textColor="@android:color/white" />
</LinearLayout>
<Button
android:id="@+id/button_action_prev"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="@color/people_app_theme_color"
android:gravity="center"
android:text="PREV"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="@android:color/white" />
和我根本就不能得到的按钮( button_action_next
和 button_action_ $ P $光伏
垂直居中内操作栏)
And I simply just can't get the buttons (button_action_next
and button_action_prev
centered vertically within the action bar)
我得到的结果是这样的:
I got the result like this:
然而,在相同的应用程序,我有ActionBarSherlock另一个自定义布局可能奇怪使按钮垂直居中很好。这个自定义布局的布局是这样的:
However, in the same app, I have another custom layout for ActionBarSherlock which could strangely make the buttons centered vertically very well. The layout of this custom layout is like this:
<Button
android:id="@+id/button_actionbar_done"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="@color/people_app_theme_color"
android:gravity="center"
android:text="DONE"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="@android:color/white" />
<Button
android:id="@+id/button_actionbar_skip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="@color/people_app_theme_color"
android:gravity="center"
android:text="SKIP"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="@android:color/white" />
<TextView
android:id="@+id/text_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button_actionbar_skip"
android:layout_alignBottom="@+id/button_actionbar_skip"
android:layout_centerHorizontal="true"
android:text="ENTER YOUR DETAILS"
android:textColor="@android:color/white" />
在工作布局和不工作布局的唯一区别,是一个不能有一个孩子的LinearLayout
在它中心的按钮。
The only difference between the working layout and not working layout, is the one that can't center buttons has a child LinearLayout
within it.
所以我的问题是,我怎么能确保该按钮垂直居中在任何情况下(避免在屏幕截图中显示的结果)?
So my question is, how can I make sure that the buttons are vertically centered in any circumstances(avoid the result shown in the screenshot)?
由于@nicopico的注释,这太帖子:机器人 - 重力和layout_gravity 一>。我申请 layout_gravity =中心
来我的布局,但按钮依然没有垂直居中。
Thanks to @nicopico 's comment and this SO post: Android - gravity and layout_gravity . I've applied layout_gravity="center"
to my layout, but the buttons are still not vertically centered.
推荐答案
确定我得到了这个问题。
OK I got the problem.
这里的解决方案
这个布局中,
<Button
android:id="@+id/button_actionbar_done"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="@color/people_app_theme_color"
android:gravity="center"
android:text="DONE"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="@android:color/white" />
<Button
android:id="@+id/button_actionbar_skip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="@color/people_app_theme_color"
android:gravity="center"
android:text="SKIP"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="@android:color/white" />
<TextView
android:id="@+id/text_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button_actionbar_skip"
android:layout_alignBottom="@+id/button_actionbar_skip"
android:layout_centerHorizontal="true"
android:text="ENTER YOUR DETAILS"
android:textColor="@android:color/white" />
我删除
安卓layout_alignParentTop =真正的
在两个按钮,并应用:
安卓layout_centerVertical =真正的
要两个按钮,它现在可以在动作条中垂直居中
to both buttons, and it now can vertically centered within the ActionBar
根据这个帖子Android动作条:如何垂直对齐的观点在上面
我可以申请的安卓layout_marginBottom
或的android:paddingBottom会
的按钮,使其中心
I can apply the android:layout_marginBottom
or android:paddingBottom
to the buttons to make it centered.
不过,我还是preFER的机器人:layout_centerVertical
方法,这是不硬coded到数
However I still prefer the android:layout_centerVertical
method which is not hard-coded to numbers.
这篇关于巴顿在ActionBarSherlock不是垂直居中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!