【问题标题】:How to place an android component offset from center?如何放置一个从中心偏移的android组件?
【发布时间】:2013-02-02 22:32:37
【问题描述】:

我想把我的图像视图放在中心上方一点。我使用 centerHorizo​​ntal="true" 和 paddingBottom ,但它位于绝对中心。 “填充”没有做这项工作。我该怎么办?

<ImageView
    android:id="@+id/imageview"
    style="@style/myImageView"
    android:layout_width="200dp"
    android:layout_height="100dp"
    android:layout_centerHorizontal="true"
    android:paddingBottom="300dp"
    android:src="@drawable/ic_launcher" />

【问题讨论】:

    标签: android xml imageview offset centering


    【解决方案1】:

    尝试使用 layout:margin_bottom 。如果没有得到你想要的。应用 relativelayout 和相同的 bottom_margin。

    【讨论】:

    • 我正在使用相对布局,但 bottom_margin 仍然不起作用。
    • 好的。我可以再给你一个建议,这肯定行得通。但这不是一种优化的方式。它拥有额外的内存。创建一个位于布局中心的假视图,可见性消失.现在在您看来,您希望稍微高于中心放置 android:layout_above="@+id/id_of_invisible view_which_is_in_center_we_just_created"。使用这个技巧你会得到你想要的。
    • 感谢您的想法。我在相对布局内创建了一个框架布局,定位了 imageview,然后将重力设置为中心。
    【解决方案2】:

    找不到确切的答案,但找到了另一种方法。我在相对布局内创建了一个框架布局,将图像视图放置在其中,然后将重力设置为中心。 如果有更好的解决方案请留言。 我的代码:

    Inside the relative layout--
     <FrameLayout
        android:layout_width="800dp"
        android:layout_height="400dp"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true" 
        android:layout_marginRight="30dp"
        android:layout_marginBottom="100dp"
    
        >
        <ImageView
        android:id="@+id/imageview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:src="@drawable/ic_launcher"
        style="@style/myImageView" />
    </FrameLayout>
    

    【讨论】:

      【解决方案3】:

      我希望您使用的是相对布局,而不是线性或绝对布局。

      【讨论】:

      • 是的,我使用的是相对布局。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-01-25
      • 2013-09-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-17
      • 1970-01-01
      相关资源
      最近更新 更多