如何按中心对齐按钮并进行偏移

时间:2012-11-21 20:51:42

标签: android layout android-view android-button

我想在中心对齐按钮并制作偏移我尝试接近Is there a way to offset a view from center in Android?,但它不起作用。例如:

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

仍然留在中心

有没有办法像这样完成:

enter image description here

更新:

我的完整布局:

<?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>

1 个答案:

答案 0 :(得分:11)

一种方法是使用以相对布局为中心的锚点视图,在其右侧设置按钮并设置边距。

 <?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>