Android Studio 如何为片段内的视图设置约束

Android Studio how to set constraints for a view inside a fragment

正在尝试将视图附加到片段的左下角,但它不遵守我的命令:(

代码编译运行,但按钮非常位于屏幕的左上角。

<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_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MapsActivity">


    <ImageButton
        android:id="@+id/searchBtn"
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:adjustViewBounds="true"
        android:background="@null"
        android:scaleType="centerInside"
        map:layout_constraintBottom_toBottomOf="parent"
        map:layout_constraintStart_toStartOf="parent"
        map:srcCompat="@drawable/loupe"/>
</fragment>

android studio 的新手,还没有完全理解所有的模式,所以我觉得我可能试图以错误的方式做事。

我是否应该定义某种容器,然后将地图片段和按钮放入其中?

但如果我这样做,它会破坏我的主地图代码。

不确定您在问什么,但据我了解,您希望将 ImageButton 放置在片段之上。 您可以将此添加到您的按钮 android:layout_gravity="bottom|start" 以定位您的按钮。

使用线性布局更好。它将元素水平并排放置或垂直放置,具体取决于您在 android:orientation=""

中所说的内容
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MapsActivity">
    <ImageButton
    android:id="@+id/searchBtn"
    android:layout_width="150dp"
    android:layout_height="150dp"
    android:background="@null"/>
</LinearLayout>

然后,如果你想让它在左下角,你可以在ImageButton中添加android:layout_marginBottom="0dp"android:layout_marginLeft="0dp"。这意味着 ImageButton 应该距离屏幕底部 0dp,也应该距离屏幕左侧 0dp,这使得它位于左下角。

感谢@Tyler 的评论,我不能接受它作为答案,因为它是评论。

"The commands layout_constraintBottom_toBottomOf and layout_constraintStart_toStartOf are for a ConstraintLayout. If you want them to be used, you need to put the view inside a ConstraintLayout. However, check out this question on how to lay out a fragment. The fragment layout shouldn't go inside the tag, it should be its own file."

固定的 xml 代码如下所示(无需更改任何其他代码)-

<androidx.constraintlayout.widget.ConstraintLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MapsActivity">

    <fragment
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/map_fragment"
        android:name="com.google.android.gms.maps.SupportMapFragment" />

    <ImageButton
        android:id="@+id/searchBtn"
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:adjustViewBounds="true"
        android:background="@null"
        android:scaleType="centerInside"
        map:layout_constraintBottom_toBottomOf="@id/map"
        map:layout_constraintStart_toStartOf="@id/map"
        map:srcCompat="@drawable/loupe"/>

</androidx.constraintlayout.widget.ConstraintLayout>