代码之家  ›  专栏  ›  技术社区  ›  Rob N

为什么根元素的布局属性没有膨胀?

  •  0
  • Rob N  · 技术社区  · 5 年前

    我有一个 RecyclerView 回收视图

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyHolder {
        val binding = MyCellBinding.inflate(layoutInflater)  
        // here, binding.root.layoutParams is null ??      
        return MyHolder(binding)
    }
    

    MyHolder 上课照常 ViewHolder 子类。

    这个 MyCellBinding 来自 layout/my_cell.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"  
        android:orientation="vertical"
        android:layout_width="match_parent"   ← ✅
        android:layout_height="match_parent"  ← ✅
        android:padding="8dp">
    
        <TextView android:id="@+id/title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    
        ...
    </LinearLayout>
    

    这个 layout_width/height TextView 正确创建,但由于某些原因,没有 LayoutParams binding.root - LinearLayout . 因此,当它显示在列表中时,它与 文本框

    我可以使用LayoutSpector查看 线性布局 回收视图 .

    layout_width layout_height 线性布局 没有从XML中膨胀?

    0 回复  |  直到 5 年前