我有这个复合 View ArticleView
,
当我将根LinearLayout
替换为merge
时,该 View 在我的测试设备或仿真器上均不可见。为什么会这样?ArticleView.kt
:
import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.LinearLayout
import com.kaf.hajjcompanion.R
import kotlinx.android.synthetic.main.article_view.view.*
import net.cachapa.expandablelayout.ExpandableLayout.State.EXPANDED
class ArticleView @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0
) : LinearLayout(context, attrs, defStyleAttr) {
private val header by lazy { question_header }
private val paragraph by lazy { paragraph_view }
private val expandableParagraph by lazy { expandable_paragraph }
init {
inflate()
}
private fun inflate() {
LayoutInflater.from(context).inflate(R.layout.article_view, this, true)
header.setOnClickListener {
expandableParagraph.toggle()
}
expandableParagraph.setOnExpansionUpdateListener { _, state ->
header.setCompoundDrawablesRelativeWithIntrinsicBounds(
0, 0,
if (state == EXPANDED) R.drawable.ic_expand_less_white_24dp else R.drawable.ic_expand_more_white_24dp,
0
)
}
}
fun render(viewState: ArticleViewState) {
with(viewState) {
header.setText(title)
paragraph.text = paragraphText
}
}
}
article_view.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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="@dimen/media_width"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/question_header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/hajj_black"
android:drawableEnd="@drawable/ic_expand_more_white_24dp"
android:elevation="@dimen/default_elevation"
android:padding="8dp"
android:textAlignment="viewStart"
android:textColor="@android:color/white"
android:textSize="@dimen/entry_title_size"
tools:text="@string/hajj_comp_how_to"
/>
<net.cachapa.expandablelayout.ExpandableLayout
android:id="@+id/expandable_paragraph"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:el_duration="500"
app:el_expanded="false">
<com.kaf.hajjcompanion.ui.commons.EmbeddedParagraphView
android:id="@+id/paragraph_view"
android:layout_width="match_parent"
android:layout_height="@dimen/paragraph_height"/>
</net.cachapa.expandablelayout.ExpandableLayout>
</LinearLayout>
在我的片段中:
private fun renderState(state: SectionState) {
when (state) {
//..
is SectionState.Article -> ArticleView(context!!).run {
render(state.what)
root.addView(this)
}
//..
}
}
分段:
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="@dimen/media_width"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_gravity="center"
android:scrollbars="none">
<LinearLayout
android:id="@+id/root"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="@drawable/entries_divider"
android:orientation="vertical"
android:showDividers="middle"/>
</ScrollView>
请注意,我使用的不是
RecyclerView
,因为我处理的静态数据少于5个元素。 最佳答案
可能是因为您丢失了根参数:
android:layout_width="@dimen/media_width"
android:layout_height="match_parent"
android:orientation="vertical"
因此:
ArticleViews
的大小由封闭布局的generateDefaultLayoutParams()
决定。 LinearLayout
的默认方向,如果没有记错的话,它是horizontal
。