【问题标题】:How to align Button by center and make offset如何按中心对齐按钮并进行偏移
【发布时间】:2012-11-21 20:51:41
【问题描述】:

我想在中心对齐按钮并进行偏移我尝试过类似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" />

仍然保持中心位置

有没有办法像这样完成:

更新:

我的完整布局:

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

【问题讨论】:

  • 居中,然后使用android:layout_marginLeft="" 将其移到任意远的位置
  • @BillGary 看看更新它还是不行。

标签: android layout android-view android-button


【解决方案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>

【讨论】:

    猜你喜欢
    • 2014-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-04
    • 2015-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多