4

ボタンを中央に揃えてオフセットさせたいそのようなアプローチを試しましたAndroidで中央からのビューをオフセットする方法はありますか?しかし、それは機能しません。例えば:

<View
android:id="@+id/fakeView"
android:layout_width="10dp"
android:layout_height="10dp"
android:layout_centerInParent="true"
android:paddingTop="80dp"
android:background="#FFAABB" />

まだ中心にとどまる

このように達成する方法はありますか?

ここに画像の説明を入力してください

アップデート:

私の完全なレイアウト:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
     <ImageButton 
         android:layout_width="100dp"
         android:layout_height="100dp"
         android:layout_centerHorizontal="true"
         android:layout_marginLeft="100dp"
         />
</RelativeLayout>
4

1 に答える 1

11

1つの方法は、相対レイアウトの中央に配置されたアンカービューを使用し、ボタンをその右側に設定して、マージンを設定することです。

 <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <View
            android:id="@+id/anchor"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_centerInParent="true" />

         <ImageButton 
             android:layout_width="100dp"
             android:layout_height="100dp"
             android:layout_marginRight="50dp"
             android:layout_toRightOf="@+id/anchor"
             />


    </RelativeLayout>

または試してみてください:

 <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center"  >

         <ImageButton 
             android:layout_width="100dp"
             android:layout_height="100dp"
             android:layout_marginRight="50dp"
             />

    </RelativeLayout>
于 2012-11-21T21:12:48.920 に答える