2015-09-27 109 views
1

我有2个按钮和一个水平线性布局内的TextView。所有三个视图都有权重。我一直没有成功将两个按钮垂直居中。不能垂直居中按钮

我曾尝试:

android:layout_gravity="center" 

android:gravity="center" 

线性布局

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:background="#707070" 
    > 

    <Button 
     android:id="@+id/ratioLBButton" 
     android:text="LB" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:layout_weight="1" 
     android:onClick="ratioLBFunction" 
     /> 

    <Button 
     android:id="@+id/ratioKGButton" 
     android:text="KG" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:layout_weight="1" 
     android:onClick="ratioKGFunction" 
     /> 

    <TextView 
     android:text="Hello World" 
     android:textSize="18sp" 
     android:gravity="center" 
     android:id="@+id/ratioOutput" 
     android:background="#707070" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:layout_weight="5" 
     /> 

</LinearLayout> 

这是包括上述意见父布局。

<ScrollView 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools"> 

<LinearLayout 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context=".MainActivity" 
android:orientation="vertical" 
android:background="#989898" 
> 

这是我的平板电脑的屏幕截图。这是我的android手机完全相同。

enter image description here

+1

你的代码显示的是彼此相邻的按钮,你是否试图让它们在彼此之上?同时保持水平条? – nbroeking

+1

或让它们居中水平布局 – nbroeking

+0

水平相邻,但垂直居中在线性布局 – th3ramr0d

回答

0

这可能是一个愚蠢的答案。但是,您是否在<LinearLayout .....>开始标记中添加了xmlns:android="http://schemas.android.com/apk/res/android"?如果不是,请添加它。你的XML工作正常,我不能重现这个问题。

+0

我确实有这个标签。这个线性布局嵌套在另一个布局中。 – th3ramr0d

+0

你是否也可以发布父级布局?它确实会影响子布局如何反应 –

+0

我添加了父布局 – th3ramr0d

1

如果您使用的是weight,那么您需要将android:weightSum="10"添加到父级,这将有助于父级知道应分配给每个级别的空间的百分比。权重总和可以是价值。也是父母的所有孩子都应该。使用android:layout_centerInParent="true"也使所有的孩子android:layout_width="fill_parent"

是这样的:

<LinearLayout 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:orientation="horizontal" 
android:weightSum="7"> 


<Button 
    android:id="@+id/ratioLBButton" 
    android:text="LB" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center" 
    android:layout_weight="1" 
    android:onClick="ratioLBFunction" 
    /> 

<Button 
    android:id="@+id/ratioKGButton" 
    android:text="KG" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center" 
    android:layout_weight="1" 
    android:onClick="ratioKGFunction" 
    /> 

<TextView 
    android:text="Hello World" 
    android:textSize="18sp" 
    android:gravity="center" 
    android:id="@+id/ratioOutput" 
    android:background="#707070" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center" 
    android:layout_weight="5" 
    /> 

每个孩子体重的总和必须等于父的weightSum。 注:较小的值将占用更多的空间

+0

我在线性布局中添加了一个权重总和,在这种情况下为7,没有任何内容。我还使用了android:layout_centerInParent =“true”,但仍然没有任何结果。这些物品按照我想要的方式水平放置。我需要他们在线性布局中垂直居中。 – th3ramr0d

0

我认为你的答案在于android:layout_weightandroid:weightSum属性。父母和孩子的布局。也就是说,如果你有一个layout_weight="1"内3次,它应该是android:weightSum="3"你的父母,因为它表明:

通知行:6

<?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="wrap_content" 
    android:weightSum="3" 
    android:background="#707070" 
    android:orientation="horizontal"> 

    <Button 
     android:id="@+id/ratioLBButton" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:layout_weight="1" 
     android:onClick="ratioLBFunction" 
     android:text="LB" /> 

    <Button 
     android:id="@+id/ratioKGButton" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:layout_weight="1" 
     android:onClick="ratioKGFunction" 
     android:text="KG" /> 

    <TextView 
     android:id="@+id/ratioOutput" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:layout_weight="1" 
     android:background="#707070" 
     android:gravity="center" 
     android:text="Hello World" 
     android:textSize="18sp" /> 

</LinearLayout> 

我的预览:

enter image description here

+0

我的预览显示的是同一件事,按钮居中。我上面说过。这就是为什么我张贴了我的设备的屏幕截图,因为它没有居中。并且在我的布局中添加android:layout_weight =“3”并没有做任何事情=( – th3ramr0d

+0

)您是否尝试过:android:weightSum =“3”?:\ –

+0

只是做了而没有。 – th3ramr0d