2012-04-06 41 views
0

hy!居中按鈕水平不起作用

我想中心我的按鈕水平,但這從來沒有工作。

我的佈局:

<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不會在中間請幫助裝按鈕。

enter image description here

回答

1

的問題是,RelativeLayout沒有跨越頁面的寬度,它的寬度是wrap_content。該按鈕居中於RelativeLayout,但該佈局只與按鈕一樣寬,並且在屏幕的左側被混合。

使內部RelativeLayout的寬度爲match_parent

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    ... 
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> 
0

這不是直接對您的問題說話,而是您的視圖層次結構太複雜。我建議您將所有LinearLayoutRelativeLayout元素替換爲涵蓋整個事物的單一RelativeLayout