【问题标题】:How to properly position elements in Android FrameLayout?如何在 Android FrameLayout 中正确定位元素?
【发布时间】:2015-11-29 19:47:44
【问题描述】:

我正在尝试将 ImageButton 覆盖在 GoogleMap 的片段之上。我也尝试使用following StackOverflow post 作为参考,但没有运气。为了做到这一点,这是我的 xml:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MapActivity" >

    <fragment xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:map="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_height="fill_parent"
        android:layout_width="fill_parent"
        tools:context="com.example.beckah.myapplication.Citisplore"/>

    <ImageButton
        android:id="@+id/imageButton1"
        android:background="@drawable/transit_icon"
        android:layout_width="55dp"
        android:layout_height="55dp"
        android:scaleType="fitCenter"
        android:layout_gravity="right|bottom" />

</FrameLayout>

产生这个视图:

我的目标是定位 ImageButton,使其看起来像这样:

我也很确定硬编码图像的高度/宽度并不是最佳做法。

如何更精确地定位 ImageButton?

【问题讨论】:

  • 看起来您需要在 ImageButton 周围留出边距

标签: android xml user-interface


【解决方案1】:

给这个按钮添加一些边距android:layout_margin="16dp"

<ImageButton
        android:id="@+id/imageButton1"
        android:background="@drawable/transit_icon"
        android:layout_width="56dp"
        android:layout_margin="16dp"
        android:layout_height="56dp"
        android:scaleType="fitCenter"
        android:layout_gravity="right|bottom" />

【讨论】:

    【解决方案2】:

    使用右边距和底部边距

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-08
      • 1970-01-01
      • 2014-11-12
      相关资源
      最近更新 更多