我有一个
如果所有按钮的总宽度小于屏幕的总宽度,则将每个按钮的宽度设置为(屏幕宽度/按钮数)。
我正在测试一些基本代码来获取/设置按钮宽度,例如在我的
recyclerview
,它只是一个水平按钮列表。我想要创建以下行为:如果所有按钮的总宽度小于屏幕的总宽度,则将每个按钮的宽度设置为(屏幕宽度/按钮数)。
我正在测试一些基本代码来获取/设置按钮宽度,例如在我的
adapter
类中: override fun onBindViewHolder(holder: TabViewHolder, position: Int) {
Log.d("TEST-APP","Button Width: " + holder.button.width.toString())
holder.button.width = 1080
Log.d("VED-APP","New Button Width: " + holder.button.width.toString())
holder.button.text = items[position].first
holder.button.setOnClickListener {
(context as MainActivity).updateCurrentImageLayout(items[position].second)
}
}
奇怪的是,虽然这段代码确实将按钮的宽度更改为1080,但在设置按钮宽度之前/之后的两个Log
函数的输出都为0。
我该如何获取按钮的实际宽度?
以下是定义我的recyclerview
中一列的布局:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent">
<Button
android:id="@+id/tabButton"
android:layout_width="match_parent"
android:minWidth="120dp"
android:layout_height="match_parent"
android:text="Unset-Button-Text"/>
</android.support.constraint.ConstraintLayout>