我正在使用 Android studio 和 kotlin。我的应用程序有一个
HorizontalScrollView
,其中包含一个 LinearLayout
以及最多无限数量的 strip_view.xml
布局(每个音频通道 1 个条带)。我的目标是使每个条带的宽度正好是 match_parent
的 1/8,这样我就可以在能够滚动的同时一次显示 8 个条带。
这就是主布局和条带布局当前的样子(显然要扩展,但只有外部容器与这个问题相关)。
我尝试了两种方法,都失败了,因为由于某种原因,我缩放的父视图(
LinearLayout
)的宽度始终保持为0,使得条带完全消失。
注意:我无法在这个项目中使用
RecyclerView
,请不要建议与之相关的解决方案。
方法1:在膨胀之前缩放视图
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val inflater = LayoutInflater.from(this)
val listOfStrips: List<Strip> = List(50) { Strip(it + 1) }
val llScrollableContent = findViewById<LinearLayout>(R.id.llScrollableContent)
val stripWidth = llScrollableContent.width / 8
for (strip in listOfStrips) {
val stripLayout = inflater.inflate(R.layout.strip_view, llScrollableContent, false)
val title: TextView = stripLayout.findViewById<TextView>(R.id.tvStripTitle)
val stripContainer: ConstraintLayout =
stripLayout.findViewById<ConstraintLayout>(R.id.clStripContainer)
val layoutParams = stripContainer.layoutParams as LayoutParams
layoutParams.width = stripWidth
stripContainer.layoutParams = layoutParams
title.text = strip.id.toString()
llScrollableContent.addView(stripLayout)
}
}
}
方法 2:膨胀布局,然后重新缩放视图。
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val inflater = LayoutInflater.from(this)
val listOfStrips: List<Strip> = List(50) {Strip(it + 1) }
val llScrollableContent = findViewById<LinearLayout>(R.id.llScrollableContent)
for(strip in listOfStrips){
val stripLayout = inflater.inflate(R.layout.strip_view, llScrollableContent, false)
val title: TextView = stripLayout.findViewById<TextView>(R.id.tvStripTitle)
val stripContainer: ConstraintLayout = stripLayout.findViewById<ConstraintLayout>(R.id.clStripContainer)
title.text = strip.id.toString()
llScrollableContent.addView(stripLayout)
}
val stripWidth = llScrollableContent.width / 8
for(i in 0 until llScrollableContent.childCount){
val childView = llScrollableContent.getChildAt(i)
val layoutParamsStrip = childView.layoutParams
layoutParamsStrip.width = stripWidth
childView.layoutParams = layoutParamsStrip
}
}
编辑:我另外添加了布局 XML 文件
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:id="@+id/clOuter"
android:layout_width="match_parent"
android:layout_height="match_parent">
<HorizontalScrollView
android:id="@+id/hsvStripeContainer"
android:layout_width="match_parent"
android:layout_height="310dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<LinearLayout
android:id="@+id/llScrollableContent"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal"></LinearLayout>
</HorizontalScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
strip_view.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:id="@+id/clStripContainer"
android:layout_width="100dp"
android:layout_height="200dp"
android:orientation="vertical">
<Button
android:id="@+id/button"
android:layout_width="84dp"
android:layout_height="82dp"
android:text="Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="@+id/tvStripTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="default text"
app:layout_constraintBottom_toTopOf="@+id/button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
我的方法不起作用的问题是因为我不知道膨胀布局并不等于绘制布局,这意味着即使在膨胀后,其元素的尺寸仍然可以== 0px。
每次需要测量 UI 元素的尺寸(例如将视图动态缩放为 UI 元素的百分比)时,都必须使用布局监听器。我不知道布局监听器的所有类型,但我使用的是
OnGlobalLayoutListener
,这是一个回调函数,每次 UI 在任何地方更新时都会被调用。