我嘗試在 item_layout 中的 recyclerView 專案之間添加空間。它適用于 XML 預覽,但不適用于設備。
city_item_layout:
<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="viewClickListener"
type="android.view.View.OnClickListener" />
<variable
name="weatherResponseBody"
type="com.example.weatherapp.model.WeatherResponseBody" />
<variable
name="city"
type="com.example.weatherapp.model.CityWeatherReport" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:onClick="@{viewClickListener}"
android:padding="20dp"
android:background="@drawable/curved_background"
android:foreground="?attr/selectableItemBackground"
android:clickable="true"
android:focusable="true"
android:elevation="1dp">
<ImageView
android:id="@ id/weather_state_icon_iv"
android:layout_width="40dp"
android:layout_height="40dp"
android:src="@drawable/ic_baseline_wb_sunny_24"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:imageUrl="@{city.weatherIcon}"/>
<TextView
android:id="@ id/city_tv"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="@{city.city}"
android:textSize="20sp"
android:textStyle="bold"
android:layout_marginStart="20dp"
app:layout_constraintStart_toEndOf="@id/weather_state_icon_iv"
app:layout_constraintTop_toTopOf="@id/weather_state_icon_iv"
tools:text="New York" />
<TextView
android:id="@ id/country_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{city.country}"
android:textStyle="italic"
app:layout_constraintEnd_toEndOf="@id/city_tv"
app:layout_constraintTop_toBottomOf="@id/city_tv"
tools:text="Nigeria" />
<TextView
android:id="@ id/weather_state_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{city.mainWeather}"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="@id/weather_state_icon_iv"
app:layout_constraintStart_toStartOf="@id/weather_state_icon_iv"
app:layout_constraintTop_toBottomOf="@id/weather_state_icon_iv"
app:layout_constraintVertical_bias="0.0"
tools:text="Sunny" />
<TextView
android:id="@ id/temperature_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="170dp"
android:text="@{city.tempToString()}"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toEndOf="@ id/country_tv"
app:layout_constraintTop_toTopOf="parent"
tools:text="@string/temperature" />
</androidx.constraintlayout.widget.ConstraintLayout>
RecyclerView_layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
tools:context=".MainFragment">
<androidx.appcompat.widget.Toolbar
android:id="@ id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:elevation="2dp"
android:minHeight="?attr/actionBarSize">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Weather Forecast"
android:textSize="18sp"
android:textStyle="bold" />
</androidx.appcompat.widget.Toolbar>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingHorizontal="20dp">
<androidx.recyclerview.widget.RecyclerView
android:id="@ id/city_list_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
tools:listitem="@layout/city_item_layout" />
</LinearLayout>
XML 預覽: 在此處輸入影像描述
在設備上安裝后:
在此處輸入圖片說明
我不知道我做錯了什么。我需要幫助
uj5u.com熱心網友回復:
我不熟悉 RecyclerView 中的填充,但它似乎忽略了它。一個肯定有效的解決方案是將您的 ConstraintLayout 包裹在另一個 ConstraintLayout 中,所有邊緣都有 20dp 的邊距。
做一些類似的事情,但使用 FrameLayout 而不是 ConstraintLayout 可能會更高效。
無論如何,我不能向您保證這是最好的解決方案,但它會起作用。
uj5u.com熱心網友回復:
嘗試用 LinearLayout 包裹你的 ConstraintLayout item_layout.xml,它應該是:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="@{viewClickListener}"
android:padding="20dp"
android:background="@drawable/curved_background"
android:foreground="?attr/selectableItemBackground"
android:clickable="true"
android:focusable="true"
android:elevation="1dp">
<!-- your code here-->
</LinearLayout>
你可以試一試,祝你好運;)
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/312850.html
標籤:安卓 xml 科特林 安卓布局 android-recyclerview
