它應該看起來如何 -

它看起來如何——

listitem.xml -
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
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="wrap_content"
app:cardBackgroundColor="@color/card_background"
android:layout_marginVertical="5dp"
android:layout_marginHorizontal="10dp"
app:cardCornerRadius="8dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@ id/is_app_or_web_image_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/card_spacing"
android:padding="@dimen/image_padding"
app:layout_constraintBottom_toBottomOf="@id/delete_image_view"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="@id/delete_image_view"
tools:src="@drawable/is_website_icon_24" />
<TextView
android:id="@ id/service_name_text_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginHorizontal="@dimen/card_spacing"
android:text="@string/service_name"
android:textSize="16sp"
app:layout_constraintBottom_toBottomOf="@id/delete_image_view"
app:layout_constraintLeft_toRightOf="@id/is_app_or_web_image_view"
app:layout_constraintRight_toLeftOf="@id/edit_image_view"
app:layout_constraintTop_toTopOf="@id/delete_image_view" />
<androidx.appcompat.widget.AppCompatImageButton
android:id="@ id/delete_image_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/card_spacing"
android:layout_marginEnd="@dimen/card_spacing"
android:background="@color/card_foreground"
android:padding="@dimen/image_padding"
android:src="@drawable/ic_baseline_delete_20"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.appcompat.widget.AppCompatImageButton
android:id="@ id/show_image_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="@dimen/card_spacing"
android:background="@color/card_foreground"
android:padding="@dimen/image_padding"
android:src="@drawable/ic_baseline_show_20"
app:layout_constraintBottom_toBottomOf="@id/delete_image_view"
app:layout_constraintRight_toLeftOf="@id/delete_image_view"
app:layout_constraintTop_toTopOf="@id/delete_image_view" />
<androidx.appcompat.widget.AppCompatImageButton
android:id="@ id/edit_image_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="@dimen/card_spacing"
android:background="@color/card_foreground"
android:padding="@dimen/image_padding"
android:src="@drawable/ic_baseline_edit_20"
app:layout_constraintBottom_toBottomOf="@id/delete_image_view"
app:layout_constraintRight_toLeftOf="@id/show_image_view"
app:layout_constraintTop_toTopOf="@id/delete_image_view" />
<androidx.cardview.widget.CardView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginVertical="@dimen/card_spacing"
app:cardBackgroundColor="@color/card_foreground"
app:cardCornerRadius="@dimen/corner_radius"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="@id/is_app_or_web_image_view"
app:layout_constraintRight_toRightOf="@id/delete_image_view"
app:layout_constraintTop_toBottomOf="@id/is_app_or_web_image_view">
<com.google.android.material.textview.MaterialTextView
android:id="@ id/service_password_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp"
android:background="#00000000"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="@string/service_password" />
</androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
我已經嘗試過的 -
將串列項的高度設定為固定值。像 200 dp 之類的東西(沒有效果。就好像那個值甚至沒有輸入一樣)
嘗試更改布局管理器(無效)
現在,我什至不知道該嘗試什么。任何幫助都是有用的。
配接器代碼 -
package com.kenetic.savepass.adapters
import android.util.Log
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.kenetic.savepass.R
import com.kenetic.savepass.databinding.PassListBinding
import com.kenetic.savepass.password.PasswordData
class PassAdapter() :
ListAdapter<PasswordData, PassAdapter.PassViewHolder>(diffCallBack) {
class PassViewHolder(val binding: PassListBinding) : RecyclerView.ViewHolder(binding.root) {
var verifiedWithPassword = false
private val TAG = "PassAdapter"
fun bind(passwordData: PasswordData) {
binding.isAppOrWebImageView.setImageResource(
if (passwordData.isAnApplication) {
R.drawable.is_application_icon_24
} else {
R.drawable.is_website_icon_24
}
)
binding.serviceNameTextView.text = passwordData.serviceName
binding.servicePasswordTextView.text = passwordData.servicePassword
binding.deleteImageView.setOnClickListener {
Log.d(TAG, "delete image onClick working")
}
binding.showImageView.setOnClickListener {
Log.d(TAG, "show image onClick working")
}
binding.editImageView.setOnClickListener {
Log.d(TAG, "edit image onClick working")
}
}
}
companion object {
private val diffCallBack = object : DiffUtil.ItemCallback<PasswordData>() {
override fun areItemsTheSame(oldItem: PasswordData, newItem: PasswordData) =
(oldItem.id == newItem.id)
override fun areContentsTheSame(oldItem: PasswordData, newItem: PasswordData) =
(oldItem == newItem)
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PassViewHolder {
return PassViewHolder(PassListBinding.inflate(LayoutInflater.from(parent.context)))
}
override fun onBindViewHolder(holder: PassViewHolder, position: Int) {
holder.bind(getItem(position))
}
}
uj5u.com熱心網友回復:
如果您想使用 margin top 和 margin_bottom 而不是 marginvertical 來使用不同的邊距 android:layout_marginVertical="5dp" android:layout_marginHorizontal="10dp",串列項將洗掉邊距 并添加此行 android:layout_margin="5dp"
uj5u.com熱心網友回復:
(回答我自己的問題)(解決)
我的串列項 xml 是卡片布局。我更改了它,使卡片布局位于約束布局內,然后將卡片布局約束到父約束布局的所有四個邊。所以,現在我的串列項 xml 是一個約束布局,里面有一個卡片布局。然后,我為卡片布局添加了邊距,它在卡片布局的邊框和約束布局之間提供了間距。所以,現在我的串列項之間在視覺上有間距。
下一個問題是約束布局背景為白色。要解決此問題,請將約束布局顏色設定為透明#00000000。就這樣。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/400118.html
標籤:安卓 xml 科特林 android-recyclerview
上一篇:未找到ListViewvar
