我创建了一个名为SectionRowView的自定义视图,该视图扩展了ConstraintLayout,它很简单:
class SectionRowView : ConstraintLayout {
constructor(context: Context?) : super(context) {
inflate(context, R.layout.section_row_view, this)
}
}
SectionRowView从sectionrowview.xml中放大视图:
<merge 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/container">
<View
android:layout_width="0dp"
android:layout_height="1dp"
android:id="@+id/topBorder" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toTopOf="parent"
android:background="#979797"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="32dp"
android:id="@+id/checkBox" android:layout_marginTop="15dp"
app:layout_constraintTop_toBottomOf="@+id/topBorder" app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="30dp"
android:text="test test test test test test"/>
</merge>
然后在一个片段中,将此自定义视图添加到TableRow
内的TableLayout
中:
class CustomizeQuizFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val view = inflater.inflate(R.layout.fragment_customize_quiz, container, false)
val tableView = view.findViewById<TableLayout>(R.id.tableView)
setupTable(tableView)
return view
}
private fun setupTable(tableView: TableLayout) {
val layoutParams = TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT)
val tableRow = TableRow(context)
tableRow.layoutParams = layoutParams
val sectionView = SectionRowView(context)
sectionView.layoutParams = layoutParams
tableRow.addView(sectionView)
tableView.addView(tableRow)
}
}
我的片段的xml布局如下:
<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/container">
<TextView
android:text="Test"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/title" app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="70dp" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:textSize="32sp" android:gravity="center_horizontal|top" android:layout_marginStart="30dp"
android:layout_marginEnd="30dp" android:textColor="#3C6FF3"/>
<TableLayout
android:layout_width="0dp"
android:layout_height="wrap_content" android:layout_marginTop="34dp"
app:layout_constraintTop_toBottomOf="@+id/title" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" android:id="@+id/tableView" android:background="#00EC0909"/>
<Button
android:text="Start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/startButton"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintEnd_toEndOf="parent" android:background="@drawable/customstartbutton"
android:textColor="#EFFBFD" android:textAllCaps="false" android:textSize="26sp"
android:layout_marginBottom="30dp" app:layout_constraintBottom_toBottomOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
运行此命令时,我可以在布局检查器中看到SectionRowView的宽度没有像所告知的那样扩展到match_parent
。它是父元素TableRow
的全屏宽度应为:
在LinearLayout中使用SectionRowView可以很好地将宽度扩展到全屏。看来只有在TableRow内部有麻烦。
我在做什么错了?