我有一個 RecyclerView,其條目是 CardView。每個 CardView 包含一個帶有 5 個 TextView 的水平 LinearLayout。無論我嘗試什么,我都無法讓 CardView 填滿螢屏的寬度。RecyclerView 的寬度與父容器(手機螢屏)相匹配,每個 CardView 與其父容器(RecyclerView 的)寬度相匹配。Android Studio 中的設計面板顯示 CardView 應該延伸到螢屏邊緣,但事實并非如此。
這是 RecyclerView 布局的 XML:`
<?xml version="1.0" encoding="utf-8"?>
<layout 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">
<data>
<variable
name="viewModel"
type="com.example.weatherapp.home.HomeViewModel"/>
</data>
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@ id/locations_list"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
android:scrollbars="vertical"
app:listData="@{viewModel.locList}"/>
</layout>
`
這是我在 RecyclerView 中每個專案布局的 XML:`
<?xml version="1.0" encoding="utf-8"?>
<layout 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">
<data>
<import type="android.view.View" />
<variable
name="location"
type="com.example.weatherapp.entity.Entry" />
</data>
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:id="@ id/cityName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{location.cityName}" />
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:text="@{location.stateName}"
android:visibility="@{location.stateVisibility}" />
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:text="@{location.countryName}" />
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:text="@{location.fTemp}" />
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:text="@{location.weather}" />
</LinearLayout>
</androidx.cardview.widget.CardView>
</layout>
`
編輯:我給 CardView 和 RecylerView 上色以顯示清晰的邊界。CardView 是紫色的,其所在的 RecyclerView 是綠松石色。

根據 Google 的 LinearLayout 檔案,我嘗試了android:layout_weight每個 TextView 的設定,將它們全部設定為 1 并將android:layout_width每個設定為 。0dp這不會將 CardView 拉伸到螢屏邊緣,而只會縮小每個 TextView。我的目標是讓 CardView 延伸到邊緣,讓 5 個 TextView 在內部均勻分布。
uj5u.com熱心網友回復:
在你的recycerview布局中,你應該將<RecyclerView標簽放在不同的布局中,例如FrameLayout,并將FrameLayout的寬度設定為match_parent。
如果您希望“5 個 TextViews 在其中均勻分布。”,您可以將 cardview 中的 <LinearLayout 標記替換為 <ConstrainLayout 標記。ConstraninLayout 具有“chain”屬性,在這種情況下會有所幫助
uj5u.com熱心網友回復:
讓 5 個 TextView 均勻分布。
您需要像這樣weightSum向您添加屬性LinearLayout:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="5"
android:orientation="horizontal">
每個TextView重量:
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="..." />
...
...
...
...
uj5u.com熱心網友回復:
好吧,我用這篇文章弄明白了。
該頁面上有兩種解決方案:一種是編輯inflate我的布局 inflater 使用的功能,另一種是將我的 CardView 包裝在 RelativeLayout 中。這兩種解決方案都適合我。我在我的串列配接器類中使用了 Kotlin 的默認inflate方法,但是當我切換到該函式的其他版本時,我能夠阻止 CardView 在錯誤的時間附加到其父級的根。這似乎是問題所在。感謝所有回復幫助我的人!
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/534246.html
