简体   繁体   English

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

[英]Android Studio how to set constraints for a view inside a fragment

Attempting to attach a view to the bottom left of a fragment and it is not obeying my commands:(试图将视图附加到片段的左下角并且它不服从我的命令:(

The code compiles and runs but the button is very much in the top left of the screen.代码编译并运行,但按钮位于屏幕的左上角。

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

New to android studio and don't yet completely understand all the patterns, so I feel like I might be attempting to do something the wrong way. android 工作室的新手,还没有完全理解所有的模式,所以我觉得我可能试图以错误的方式做某事。

Should I be defining some sort of container and then putting the map fragment and the button inside of that?我是否应该定义某种容器,然后将 map 片段和按钮放入其中?

But if I do that it brakes the map code in my main.但如果我这样做,它会在我的主代码中阻止 map 代码。

Not sure what you are asking but from what I understand is that you want to position your ImageButton on top of fragment.不知道你在问什么,但据我了解,你想 position 你的 ImageButton 在片段顶部。 You can add this to your button android:layout_gravity="bottom|start" to position your button.您可以将此添加到您的按钮android:layout_gravity="bottom|start"到 position 您的按钮。

Using LinearLayout is better.使用 LinearLayout 更好。 It puts elements horizontally next to each other or vertically, depend on what you say in android:orientation=""它将元素水平或垂直放置,取决于您在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>

Then, if you want it to be at bottom left corner, you can add android:layout_marginBottom="0dp" and android:layout_marginLeft="0dp" in the ImageButton .然后,如果您希望它位于左下角,您可以在ImageButton中添加android:layout_marginBottom="0dp"android:layout_marginLeft="0dp" This means ImageButton should be 0dp away from bottom of the screen, also should be 0dp away from left of the screen, which makes it sits at bottom left.这意味着ImageButton应该距离屏幕底部 0dp,也应该距离屏幕左侧 0dp,这使得它位于左下角。

Thanks @Tyler for your comment which i cannot accept as an answer because it is a comment.感谢@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." “命令 layout_constraintBottom_toBottomOf 和 layout_constraintStart_toStartOf 用于 ConstraintLayout。如果要使用它们,则需要将视图放在 ConstraintLayout 中。但是,请查看有关如何布置片段的问题。片段布局不应该是 go在标签内,它应该是它自己的文件。”

The fixed xml code looks like this (no need to change any of the other code) -固定的 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM