我在相对布局末尾将 OK 按钮居中时遇到问题,我已经检查了很多类似的问题,但找不到我的错误。无论我尝试什么,确定按钮仍然偏离中心。
<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>
抱歉,我无法包含屏幕截图,我是该网站的新手,没有任何代表! 有什么想法吗?
最佳答案
在 RelativeLayout
中,您必须使用 android:layout_centerHorizontal
、android:layout_centerVertical
或 android: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:gravity
、android:layout_marginLeft
和 android: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" >
关于android - 另一个相对布局居中问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14857171/