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

问题描述 投票:0回答:1

试图将一个视图附加到一个片段的左下角,但它不听从我的命令 :(

代码编译并运行,但按钮非常在屏幕的左上方。

<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的新手,还没有完全理解所有的模式,所以我觉得我可能尝试做了一些错误的事情。

我是否应该定义某种容器,然后把地图碎片和按钮放在里面?

但如果我这样做,就会使我的main中的地图代码制动。

android android-layout android-fragments android-constraintlayout
1个回答
0
投票

不知道你在问什么,但根据我的理解,你想把你的ImageButton定位在片段的顶部,你可以在你的按钮上添加以下内容 android:layout_gravity="bottom|start" 来定位你的按钮。


0
投票

使用LinearLayout更好。它将元素水平放置在彼此旁边或垂直放置,取决于你在 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>

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


0
投票

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

"layout_constraintBottom_toBottomOf和layout_constraintStart_toStartOf这两个命令是针对ConstraintLayout的。如果你想使用它们,你需要把视图放在ConstraintLayout里面。然而,请查看这个关于如何布局片段的问题。片段布局不应该放在标签里面,它应该是自己的文件。"

固定的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>
© www.soinside.com 2019 - 2024. All rights reserved.