嘿,我在安卓作業。我想在 recyclerview android 的整個視圖中適合孩子。我有水平recyclerview。我將在圖表中顯示我想要的。
方案 1
當我在回收站視圖中有更多專案時,我想在我的回收站視圖中顯示這樣的孩子。
預期產出

方案 2
當我有三個專案時,我想像這樣展示。它將在 reyclerview 中填充整個視圖。
預期產出

方案 3
當我在 reyclerview 中有兩個專案時,我需要看起來像這樣
預期產出

我得到的問題是我有 3 個專案,視圖沒有完全填充。實際上,我想像場景 2一樣將整個視圖拉伸到全寬。
實際輸出

item_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="10dp"
android:orientation="vertical">
<LinearLayout
android:id="@ id/container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="@drawable/drawable_selector"
android:orientation="vertical">
<TextView
android:id="@ id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp" />
<TextView
android:id="@ id/subText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:layout_marginBottom="10dp" />
</LinearLayout>
<RelativeLayout
android:id="@ id/tagContainer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:visibility="gone">
<TextView
android:id="@ id/tagText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingStart="10dp"
android:paddingEnd="10dp" />
</RelativeLayout>
</FrameLayout>
更新
在@Cheticamp 建議后我做了這段代碼
companion object {
fun bindView(parent: ViewGroup): XYZViewHolder {
val view = XyzLayoutBinding.inflate(
LayoutInflater.from(parent.context),
parent,
false
)
val lp = view.container.layoutParams
lp.width = parent.measuredWidth / 3
return OptionsViewHolder(
view
)
}
}
正如你所看到的,我的最后一個專案是從最后切下來的。

I think in my framelayout i Used marginEnd 10 dp is it causing issue? please refer my layout if you need more. And one more thing I didn't divide framelayout instead I take linear layout as container. I am adding my github link.
uj5u.com熱心網友回復:
您可以更改RecyclerView專案視圖的寬度onCreateViewHolder()。ViewGroup父級是RecyclerView。
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.item_view, parent, false)
val lp = view.layoutParams
// Change the width of the item view here
lp.width = parent.measuredWidth / 3 // width is 1/3 of the width of the RecyclerView
return ItemViewHolder(view)
}
uj5u.com熱心網友回復:
自定義 frameLayout 類 使類繼承 FrameLayout 或您使用的其他布局。
public class SquareFrameLayout extends FrameLayout {
public SquareFrameLayout(@NonNull Context context) {
super(context);
}
public SquareFrameLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public SquareFrameLayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public SquareFrameLayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, widthMeasureSpec);
}
}
onMeasure 方法很重要。
然后將根布局項 recyclerView 更改為 SquareFrameLayout (現在構建的類),如下所示:
<com.example.app.SquareFrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/background_rv_item"
android:orientation="vertical"
android:padding="16dp"
android:layout_margin="4dp"
xmlns:tools="http://schemas.android.com/tools">
//your items
</com.example.app.SquareFrameLayout>
uj5u.com熱心網友回復:
您可以使用 Google 提供的 flexbox-layout 庫。您可以根據需要調整專案。下載該應用程式并查看此演示是否適合您。https://github.com/google/flexbox-layout
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/441961.html
標籤:android android-layout android-recyclerview android-linearlayout android-constra
