【问题标题】:Android Studio how to set constraints for a view inside a fragmentAndroid Studio如何为片段内的视图设置约束
【发布时间】:2020-05-17 12:43:11
【问题描述】:

试图将视图附加到片段的左下角并且它不服从我的命令:(

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

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

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

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

【问题讨论】:

  • 命令layout_constraintBottom_toBottomOflayout_constraintStart_toStartOf 用于ConstraintLayout。如果要使用它们,则需要将视图放在 ConstraintLayout 中。但是,请查看此question,了解如何布置片段。片段布局不应该放在&lt;fragment&gt; 标签内,它应该是它自己的文件。

标签: android android-layout android-fragments android-constraintlayout


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

      感谢@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>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-12-17
        • 1970-01-01
        • 2019-01-04
        • 2018-04-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多