Android(Kotlin)如何按屏幕占比缩放View?实现单屏显示8个Strip
问题:HorizontalScrollView内子视图宽度设置失败
我用Android Studio+Kotlin开发应用,界面里有个HorizontalScrollView,它内部的LinearLayout需要加载大量(无限个)strip_view.xml布局(每个对应一个音频通道Strip)。我的需求是让每个Strip的宽度刚好是屏幕宽度的1/8,这样能始终一次性显示8个Strip,同时支持横向滚动。
目前主布局外层是ConstraintLayout嵌套HorizontalScrollView,内部LinearLayout为横向排列;Strip布局外层是ConstraintLayout,包含一个TextView和Button。
我试了两种方案都没成功,问题出在获取LinearLayout宽度时它始终为0,导致设置后的Strip直接消失。注意:项目不能用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>
内容的提问来源于stack exchange,提问作者MaDudeeK
相关产品推荐
相关产品推荐

