相対レイアウトでボタンを中央に配置しようとしていますが、これは可能ですか?私は重力と向きの関数を試しましたが、何もしません。
相対レイアウトでボタンを中央に配置しようとしていますが、これは可能ですか?私は重力と向きの関数を試しましたが、何もしません。
回答:
試す
android:layout_centerHorizontal="true"まさにこのように、それは私にとってはうまくいきます:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:background="#ff0000">
    <Button
        android:id="@+id/btn_mybutton"
        android:layout_height="wrap_content"
        android:layout_width="124dip"
        android:layout_marginTop="5dip"
        android:layout_centerHorizontal="true"/>
</RelativeLayout>相対レイアウトにはCENTER_IN_PARENTを使用できます。
android:layout_centerInParent="true"RelativeLayoutの中央に配置する要素に追加します
アルカディア、以下のコードを試してみてください。探している結果を得るにはいくつかの方法がありますが、これは簡単な方法の1つです。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relative_layout"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:gravity="center">
    <Button
        android:id="@+id/the_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Centered Button"/>
</RelativeLayout>RelativeLayout自体の重力を設定すると、その中に配置されているすべてのオブジェクトに影響します。この場合、それは単なるボタンです。もちろん、ここでは任意の重力設定を使用できます(center_horizontal、top、rightなど)。
以下のコードも使用できます。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relative_layout"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent">
    <Button
        android:id="@+id/the_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Centered Button"
        android:layout_centerInParent="true"/>
</RelativeLayout>要約
追加:
android:layout_centerInParent="true"以下の属性のいずれかがビューにも設定されている場合、RelativeLayoutでのみ機能します。
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"子ビューを親ビューに揃えます。「中心」は、選択した配置の軸に基づいています。
左/右->垂直
上/下->水平
ビュー内の子/コンテンツの重力の設定:
android:gravity="center"配置が設定されていない場合は、いずれにしても、親ビュー内で子が中央に配置されます。オプションで選択できます:
<!-- Axis to Center -->
android:gravity="center_horizontal"
android:gravity="center_vertical"
<!-- Alignment to set-->
android:gravity="top"
android:gravity="bottom"
android:gravity="left"
android:gravity="right"
android:gravity="fill"
...次にあります:
android:layout_gravity="center"ビュー自体をその親の中央に配置しています
そして最後に、次の属性を親ビューに追加できます。
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"android:centerInParent="true"相対レイアウトでビューを中央に配置する場合は、ビュー内の属性を使用します。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/main_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
    <TextView
        android:layout_centerInParent="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="NWE"
        android:textSize="30dp"/>
</RelativeLayout>とても簡単です。以下のコードを試してください、
<RelativeLayout
    android:id="@+id/second_RL"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/first_RL"
    android:background="@android:color/holo_blue_bright"
    android:gravity="center">
    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />
</RelativeLayout>