我的專案中有一個布局活動,其中我使用約束布局,基本上只有 3 個視圖,第一個是自定義工具列,第二個是 Recycler 視圖,最后一個是片段視圖。我將回收站視圖限制在頂部工具列和片段視圖頂部,但我進入片段下方,甚至在螢屏下方。這是我的布局代碼。
<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="match_parent"
android:layout_height="match_parent"
android:theme="@style/Theme.Music.Font">
<androidx.appcompat.widget.Toolbar
android:id="@ id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="@ id/header_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/bold"
android:text="@string/app_name"
android:textSize="26sp" />
<androidx.appcompat.widget.SearchView
android:id="@ id/searchView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
app:searchIcon="@drawable/search_icon" />
</androidx.appcompat.widget.Toolbar>
<androidx.recyclerview.widget.RecyclerView
android:id="@ id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbarThumbVertical="@drawable/scroll_bar"
android:scrollbars="vertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@ id/toolbar" />
<androidx.fragment.app.FragmentContainerView
android:id="@ id/nowPlaying"
android:name="com.example.music.NowPlaying"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
tools:layout="@layout/fragment_now_playing" />
</androidx.constraintlayout.widget.ConstraintLayout>
當我用片段約束時,它也會出現在頂部的覆寫工具列上。如圖所示。
uj5u.com熱心網友回復:
當您使用wrap_content高度或寬度時,您是說視圖應該擴展為“顯示”內容所需的大小。空中報價中的“顯示”是因為如果有很多內容,部分視圖可能最終會從螢屏上消失。
您的約束不限制這里的大小,只是定位。如果你將一個邊緣限制在某物上,你會固定它,它會在另一端從螢屏上展開。如果你約束相反的邊緣,那么你基本上是圍繞這些約束來集中它。
如果要匹配這些約束,則需要將高度設定為0dp. 這樣,您的受約束邊緣實際上將固定在您想要的位置,從而使視圖具有一定的高度。它適合其約束邊??緣之間的空間。
查看您的布局,我猜底部的“正在播放”部分是您的FragmentContainerView. 設定為,wrap_content但它的內容似乎是固定大小,所以沒關系 - 正是你想要的!工具列也是如此 - 它是wrap_content,但它的內容是固定大小的。
因此,您RecyclerView還需要將其底部限制在 的頂部,nowPlaying并且需要 alayout_height的0dp。這樣,它被限制在工具列和正在播放的位之間的空間中,并且它的大小填充了該區域。另外兩個占用了他們需要的空間,并RecyclerView填充了剩下的空間——這就是你想要的,對吧?
通常這就是它對任何滾動的作業方式,例如布局中的RecyclerView或ScrollView- 布局中的視圖是滾動內容的視窗,因此視圖的實際大小(layout_height和layout_width) 控制該視窗的大小。內容只是在它后面上下滾動。如果該視窗與內容的大小相同wrap_content,則整個內容都是可見的(但可能在螢屏之外)并且沒有可滾動的內容!所以對于滾動的東西,你總是想限制視圖的大小。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/525284.html
上一篇:為什么在kotlin"something!=null||return"中不執行智能轉換,但"if(something==null)return"是
