在android中使用分层布局与中心对齐
为什么下面的代码不与中心对齐? 我要怎么做才能修好它在android中使用分层布局与中心对齐,android,android-layout,Android,Android Layout,为什么下面的代码不与中心对齐? 我要怎么做才能修好它 <AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_gravity="center" android:id="@+id/parent" android:layout_height="match_parent" android:layout_width="match_
<AbsoluteLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_gravity="center"
android:id="@+id/parent"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:background="@color/blue">
<RelativeLayout
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_width="wrap_content">
<Button
android:layout_gravity="center"
android:id="@+id/Question01"
android:text="12 + 23"
android:gravity="center_vertical|center_horizontal"
android:layout_height="70px"
android:lines="1"
android:textSize="40px"
android:layout_alignWithParentIfMissing="true" android:background="@drawable/orange_button" android:layout_margin="5px" android:layout_width="230px" android:textColor="@color/blue"/>
</RelativeLayout>
</AbsoluteLayout>
嗯,
android:layout\u gravity
不是关于子视图的定位,而是关于当前视图在父视图中的定位。尝试将android:gravity用于AbsoluteLayout
(您也可能希望切换到FrameLayout
,而不是abdoute)。对于RelativeLayout
,使用按钮上的android:layout\u centerInParent=“true”
<AbsoluteLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:gravity="center"
android:id="@+id/parent"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:background="@color/blue">
<RelativeLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content">
<Button
android:layout_gravity="center"
android:id="@+id/Question01"
android:text="12 + 23"
android:gravity="center_vertical|center_horizontal"
android:layout_height="70px"
android:lines="1"
android:textSize="40px"
android:layout_alignWithParentIfMissing="true"
android:background="@drawable/orange_button"
android:layout_margin="5px" android:layout_width="230px"
android:textColor="@color/blue"
android:layout_centerInParent="true"/>
</RelativeLayout>
</AbsoluteLayout>
嗯,
android:layout\u gravity
不是关于子视图的定位,而是关于当前视图在父视图中的定位。尝试将android:gravity用于AbsoluteLayout
(您也可能希望切换到FrameLayout
,而不是abdoute)。对于RelativeLayout
,使用按钮上的android:layout\u centerInParent=“true”
<AbsoluteLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:gravity="center"
android:id="@+id/parent"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:background="@color/blue">
<RelativeLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content">
<Button
android:layout_gravity="center"
android:id="@+id/Question01"
android:text="12 + 23"
android:gravity="center_vertical|center_horizontal"
android:layout_height="70px"
android:lines="1"
android:textSize="40px"
android:layout_alignWithParentIfMissing="true"
android:background="@drawable/orange_button"
android:layout_margin="5px" android:layout_width="230px"
android:textColor="@color/blue"
android:layout_centerInParent="true"/>
</RelativeLayout>
</AbsoluteLayout>