0

喂!

我想将我的按钮水平居中,但这永远行不通。

我的布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"  >


    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/minage"
        android:layout_alignParentLeft="true"
        android:padding="5dp"  />

    <TextView
            android:id="@+id/ml_minage_value"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true" 
            android:padding="5dp"/>

    </RelativeLayout>


    <SeekBar
        android:id="@+id/ml_minage"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:max="100"
        style="@style/NFFSeek"
        android:progressDrawable="@drawable/myseekbar"
        android:layout_margin="5dp" />

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/maxage" 
        android:padding="5dp"/>
    <TextView
            android:id="@+id/ml_maxage_value"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:padding="5dp" />

    </RelativeLayout>

    <SeekBar
        android:id="@+id/ml_maxage"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        style="@style/NFFSeek"
        android:progressDrawable="@drawable/myseekbar" 
        android:layout_margin="5dp" />

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/distance"
        android:padding="5dp" />

    <TextView
            android:id="@+id/ml_distance_value"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:padding="5dp" />
    </RelativeLayout>

    <SeekBar
        android:id="@+id/ml_distance"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        style="@style/NFFSeek"
        android:progressDrawable="@drawable/myseekbar" 
        android:layout_margin="5dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/gender"
        android:padding="5dp" />

    <RadioGroup
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checkedButton="@drawable/rbon" >

        <RadioButton
            android:id="@+id/ml_female"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:button="@drawable/rbselector"
            android:checked="true"
            android:drawablePadding="10dp"
            android:text="@string/rb_female"
            android:textColor="#000000"
            android:paddingLeft="42dp" />

        <RadioButton
            android:id="@+id/ml_male"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/rb_male"
            android:button="@drawable/rbselector"
            android:layout_margin="5dp"
            android:textColor="#000000"
            android:paddingLeft="42dp"
            />
    </RadioGroup>

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

    <Button
        android:id="@+id/ml_stbutton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="15dp"
        android:background="@drawable/custom_button"
        android:layout_alignParentBottom="true"
        android:layout_centerInParent="true"
        android:layout_alignParentTop="true"
        android:gravity="center"
        android:padding="10dp"
        android:text="Flirt"
        android:textColor="#ffffff"
        android:textSize="12pt"
        android:typeface="serif" />

    </RelativeLayout>

</LinearLayout>

此 xml 不适合中间的按钮,请帮忙。

在此处输入图像描述

4

3 回答 3

1

问题是RelativeLayout不跨越页面的宽度,它的宽度是wrap_content. 按钮在 中居中RelativeLayout,但该布局仅与按钮一样宽,并且与屏幕左侧混搭在一起。

使内部RelativeLayout的宽度为match_parent

<RelativeLayout
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  ...
于 2012-04-06T13:41:02.823 回答
0
<RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

    <Button
        android:id="@+id/ml_stbutton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="15dp"
        android:background="@drawable/custom_button"
        android:layout_centerInParent="true"
        android:gravity="center"
        android:padding="10dp"
        android:text="Flirt"
        android:textColor="#ffffff"
        android:textSize="12pt"
        android:typeface="serif" />

    </RelativeLayout>
于 2012-04-06T13:40:37.170 回答
0

这不是直接回答您的问题,但是您的视图层次结构太复杂了。我建议你用一个跨越整个事物的单个元素替换所有的LinearLayout和元素。RelativeLayoutRelativeLayout

于 2012-04-06T13:45:20.060 回答