拨开荷叶行,寻梦已然成。仙女莲花里,翩翩白鹭情。
IMG-LOGO
主页 文章列表 Android布局问题,ImageView未出现在所需位置(在其他两个视图之间)

Android布局问题,ImageView未出现在所需位置(在其他两个视图之间)

白鹭 - 2022-03-15 1950 0 0

我是 android 开发的新手,我正在尝试制作一个类似于这样的荧屏(布局):一个微调器,顶部有两个文本栏位,左侧下方有一个缩略图(ImageView),以及一个片段容器下面的视图我将用来显示不同的页面。现在我不是在寻找漂亮的设计,我只是想让 android 像这样显示它。

问题是,即使在 android studio 的设计选项卡上缩略图显示在正确的位置,在运行时也是完全错误的:

这是组件树和布局的 xml,以防万一它有用:

<androidx.coordinatorlayout.widget.CoordinatorLayout 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:id="@ id/activity_note"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".NoteActivity">

<com.google.android.material.appbar.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="top"
    android:theme="@style/Theme.NoteKeeper.AppBarOverlay">

    <androidx.appcompat.widget.Toolbar
        android:id="@ id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/Theme.NoteKeeper.PopupOverlay" />

</com.google.android.material.appbar.AppBarLayout>

<include
    android:id="@ id/include"
    layout="@layout/content_note" />

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="500dp"
    android:layout_gravity="bottom|center">

    <ImageView
        android:id="@ id/imageView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center|left"
        android:layout_marginStart="4dp"
        app:layout_constraintBottom_toTopOf="@ id/fragmentContainerView2"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:srcCompat="@tools:sample/avatars" />

    <androidx.fragment.app.FragmentContainerView
        android:id="@ id/fragmentContainerView2"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="367dp"
        android:layout_gravity="bottom|center"
        app:defaultNavHost="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:navGraph="@navigation/nav_graph" />
</androidx.constraintlayout.widget.ConstraintLayout>

<com.google.android.material.floatingactionbutton.FloatingActionButton
    android:id="@ id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    android:layout_marginEnd="@dimen/fab_margin"
    android:layout_marginBottom="16dp"
    app:srcCompat="@android:drawable/ic_menu_camera" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>

编辑:

最后,我不得不使用这样的约束布局:

<androidx.constraintlayout.widget.ConstraintLayout 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:id="@ id/activity_note"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".NoteActivity">


    <com.google.android.material.appbar.AppBarLayout
        android:id="@ id/appBarContentNoteLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
        android:theme="@style/Theme.NoteKeeper.AppBarOverlay"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <androidx.appcompat.widget.Toolbar
            android:id="@ id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/Theme.NoteKeeper.PopupOverlay" />

    </com.google.android.material.appbar.AppBarLayout>


    <include
        android:id="@ id/include"
        layout="@layout/content_note"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@ id/appBarContentNoteLayout" />

    <ImageView
        android:id="@ id/imageView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@ id/include"
        tools:srcCompat="@tools:sample/avatars" />

    <androidx.fragment.app.FragmentContainerView
        android:id="@ id/fragmentContainerView"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:background="#AC1A1A"
        android:backgroundTint="#C51111"
        app:defaultNavHost="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toBottomOf="@ id/imageView"
        app:navGraph="@navigation/nav_graph" />

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@ id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_marginEnd="32dp"
        android:layout_marginBottom="32dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:srcCompat="@android:drawable/ic_menu_camera" />


</androidx.constraintlayout.widget.ConstraintLayout>

添加了色调,以便片段视图显示边框

uj5u.com热心网友回复:

问题是您在 xml 代码中使用了 layout_gravity,该代码适用于某些视图组,例如FrameLayout但不适用于ConstraintLayout. 第二个事实是您不需要使用嵌套的 ConstraontLayouts,一个将处理您所有的 ui 要求。因此,对于AppBar您可以使用的第一个app:layout_constraintTop_toTopOf="parent"和内容注释,您也可以将其限制在 appBar 的底部。像这样:app:layout_constraintTop_toBottomOf="@ id/appBar"对于影像缩略图,也可以使用 this:app:layout_constraintTop_toBottomOf="@ id/contentNote"app:layout_constraintStart_toStartOf="parent"最后一个作为 fragmentContainer一起使用,您可以使用 this app:layout_constraintTop_toBottomOf="@ id/imageView"这样,所有这些都将相互约束并且运行良好。对于与父布局相关的任何其他约束,您也可以添加它们的约束。

uj5u.com热心网友回复:

您的约束在ImageView.

尝试摆脱以下行

app:layout_constraintTop_toTopOf="parent"

在您ImageView为这行代码试图影像的顶部拉一路父的顶部。

如果可行的话。否则,我可以向您发送完整的布局代码片段。

标签:

0 评论

发表评论

您的电子邮件地址不会被公开。 必填的字段已做标记 *