高度的wrap_content在LinearLayout childView

时间:2018-02-23 15:15:06

标签: android android-layout

我希望以编程方式将视图添加到LinearLayout。我创建了从xml(layout_height="wrap_content")膨胀的视图,然后将此视图添加到LinearLayout

但是当我运行我的应用时,我在LinearLayout中只看到一个子视图,此视图的高度为match_parent(子视图填充所有LinearLayout)。< / p>

如何解决?

我的代码: 将子视图添加到LinearLayout

llContent.removeAllViewsInLayout()
for (i in 1..4) {
    val view = nextCellView(context!!) // here I create my childView
    llContent.addView(view)
}

在nextCellView()中......我创建了这个视图:

abstract class AbsModuleCellView : FrameLayout {
    constructor(context: Context) : super(context) {
        initView()
    }
    // ... all empty constructors with initView() call

    @LayoutRes
    protected abstract fun obtainLayoutResId(): Int

    private fun initView() {
        inflateViewContent(obtainLayoutResId())
    }

    private fun inflateViewContent(@LayoutRes layoutResId: Int) {
        obtainInflater().inflate(layoutResId, this)
    }

    private fun obtainInflater() = context.provideLayoutInflater()
}

和xml

<LinearLayout
        android:id="@+id/llContent"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="16dp"
        android:orientation="vertical" />

对于每个创建的childView,我都应用此layoutParams

childView.layoutParams = LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)

每个子视图都是扩展FrameLayout的类,而在xml中,根视图是:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/rlBaseModuleCell"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:clickable="true"
    android:background="?selectableItemBackground"
    tools:ignore="KeyboardInaccessibleWidget">

0 个答案:

没有答案