2012-04-03 135 views
1

为什么这些垂直居中?我正在为此苦苦挣扎,无法让它工作。Android垂直中心

这是在一个相关布局和显示器的底部。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="horizontal" 
android:layout_width="fill_parent" 
android:layout_height="46dp" 
style="?headerfooter" 
android:id="@+id/linlay2" 
android:layout_below="@+id/ListView01" 
android:gravity="center_vertical" 
android:layout_alignBottom="@+id/mainrel"> 
<EditText 
    android:id="@+id/EditText_AddNewList" 
    android:layout_height="wrap_content" 
    android:layout_width="wrap_content" 
    android:hint="@string/dialog_addnew_lists_listnamehint" 
    android:layout_centerVertical="true" 
    android:gravity="center_vertical" 
    android:layout_gravity="center_vertical" 
    android:layout_weight="1"> 
</EditText> 
<Button 
     android:id="@+id/bSQLUpdate" 
     android:layout_width="40dp" 
     android:layout_height="40dp" 
     android:layout_centerVertical="true" 
     android:layout_gravity="center_vertical" 
     android:gravity="center_vertical" 
     android:layout_marginLeft="2dp" 
     android:layout_marginRight="2dp" 
     style="?button_add"/> 
</LinearLayout> 

enter image description here

+0

不知道这是否帮助,但我发现,当事情没有在Android上铺设了适当的权重属性设置为1有时帮助。 android:layout_weight =“1”。试试你的按钮。 – PaulG 2012-04-03 20:39:15

回答

0

两个建议:

1)如果你设置你的LinearLayoutlayout_height为wrap_content?
2)我怀疑你的EditText可能有点太大(我之前遇到过类似的问题),所以如果你将它的高度从wrap_content更改为match_parent,你可能会有更好的运气。

+0

1.没有区别2. match_parent不适用于我,我正在使用2.1 API 7,8需要 – erdomester 2012-04-03 21:01:00

+0

'fill_parent'和'match_parent'解析为相同的东西。 'match_parent'只是一个更准确的描述。所以,试试'fill_parent'! :) – 2012-04-03 21:05:49

+0

您也可以尝试在'EditText'上设置'layout_height'(也可能是'layout_width')为'0dp'。基本上,这将使它使用您已经分配的1的layout_weight,以便在所有其他布局完成后填充剩余空间。 – 2012-04-03 21:09:07

0

您是否尝试过使用“center”而不是“center_vertical”?另外,如果您想要将屏幕中心放在从屏幕顶部到屏幕底部的虚拟线上,center_horizo​​ntal就是您要查找的内容。

下面是一些代码,放置一个项目死在屏幕的中间:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

     <ProgressBar 
     android:id="@+id/electionsProgressBar" 
     style="?android:attr/progressBarStyleLarge" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" android:layout_gravity="center_vertical|center_horizontal"/> 

</LinearLayout> 
+0

我知道如何居中。这就是为什么我不明白为什么这不起作用 – erdomester 2012-04-03 21:02:40