我是 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為這行代碼試圖影像的頂部拉一路父的頂部。
如果可行的話。否則,我可以向您發送完整的布局代碼片段。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/396194.html
