【问题标题】:another relative layout centering issue另一个相对布局居中问题
【发布时间】:2013-01-29 05:06:20
【问题描述】:

我在相对布局末尾的确定按钮居中时遇到问题,我检查了很多类似的问题,但找不到我的错误。无论我尝试什么,确定按钮仍然偏离中心。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/frag_choix_surface"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:gravity="center"
android:orientation="vertical" >

<TextView
    android:id="@+id/textViewNbPieces"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="10dp"
    android:layout_marginTop="10dp"
    android:text=""
    android:textStyle="bold" />

<LinearLayout
    android:id="@+id/pieces"
    android:layout_marginTop="20dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textViewSurface"
    android:gravity="center"
     >

    <TextView
        android:id="@+id/textViewChmin"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_marginLeft="30dp"
        android:layout_marginRight="10dp"
        android:gravity="center"
        android:text="min :" />

    <kankan.wheel.widget.WheelView android:id="@+id/nb_piece_min"
        android:layout_height="wrap_content"
        android:layout_width="50dp"/>

    <TextView
        android:id="@+id/textViewChmax"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:gravity="center"
        android:text="max :" >

    </TextView>

    <kankan.wheel.widget.WheelView android:id="@+id/nb_piece_max"
        android:layout_height="wrap_content"
        android:layout_width="50dp"
        android:layout_marginRight="40dp"/>

</LinearLayout>

<Button
    android:id="@+id/buttonOkNbPieces"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/pieces"
    android:layout_marginTop="20dp"
    android:text="OK" />

</RelativeLayout>

抱歉,我无法提供屏幕截图,我是该网站的新手,没有任何代表! 有什么想法吗?

【问题讨论】:

    标签: android layout center android-relativelayout centering


    【解决方案1】:

    RelativeLayout 中,您必须使用android:layout_centerHorizontalandroid:layout_centerVerticalandroid:layout_centerInParent 属性。

    将xml的按钮部分改为:

    <Button
        android:id="@+id/buttonOkNbPieces"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/pieces"
        android:layout_marginTop="20dp"
        android:layout_centerHorizontal="true"
        android:text="OK" />
    

    并从父RelativeLayout中删除属性android:gravityandroid:layout_marginLeftandroid:layout_marginRight,这些属性会影响内部项目的中心位置。所以RelativeLayout 应该这样定义:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/frag_choix_surface"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="10dp" >
    

    【讨论】:

    • 感谢您的快速回答,我已经试过了,只是重新检查了一下。但按钮仍然没有移动到中心...
    【解决方案2】:
    Hope i answered your question.
    
        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/frag_choix_surface"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:layout_marginBottom="10dp"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:orientation="vertical" >
    
    <TextView
        android:id="@+id/textViewNbPieces"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="10dp"
        android:text=""
        android:textStyle="bold" />
    
    <LinearLayout
        android:id="@+id/pieces"
        android:layout_marginTop="20dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textViewSurface"
        android:gravity="center"
         >
    
        <TextView
            android:id="@+id/textViewChmin"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_marginLeft="30dp"
            android:layout_marginRight="10dp"
            android:gravity="center"
            android:text="min :" />
    
        <kankan.wheel.widget.WheelView android:id="@+id/nb_piece_min"
            android:layout_height="wrap_content"
            android:layout_width="50dp"/>
    
        <TextView
            android:id="@+id/textViewChmax"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:gravity="center"
            android:text="max :" >
    
        </TextView>
    
        <kankan.wheel.widget.WheelView android:id="@+id/nb_piece_max"
            android:layout_height="wrap_content"
            android:layout_width="50dp"
            android:layout_marginRight="40dp"/>
    
    </LinearLayout>
    
    <Button
    android:layout_marginLeft="111dp"
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/pieces"
        android:layout_marginTop="14dp"
        android:text="Button" />
    
    </RelativeLayout>
    

    【讨论】:

    • 按钮确实居中,但不再是线性布局(左侧偏移)...
    • 简单修复,只需将android:layout_centerHorizontal="true" 添加到LinearLayout。您可能还需要更新内部TextViews 的边距。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多