MaNa Asked:2020-01-30 00:11:50 +0000 UTC2020-01-30 00:11:50 +0000 UTC 2020-01-30 00:11:50 +0000 UTC 不同宽度的物体 772 这样的问题,任务是用数据做一个类似表格的东西,但是列的数量应该取决于内容的宽度,这里是一个例子: 可以使用哪些组件以及如何执行此操作?如果可能的话,举个例子 java 1 个回答 Voted Best Answer MaNa 2020-01-30T01:18:55Z2020-01-30T01:18:55Z 您需要使用 FlexboxLayout。 将依赖项添加到 Gradle: implementation 'com.google.android:flexbox:2.0.1' 添加FlexboxLayout到布局: <com.google.android.flexbox.FlexboxLayout android:id="@+id/type" app:flexWrap="wrap" app:alignItems="stretch" app:alignContent="stretch" android:layout_marginTop="10dp" android:layout_marginStart="20dp" android:layout_marginEnd="20dp" android:layout_width="match_parent" android:layout_height="wrap_content" app:layout_constraintTop_toBottomOf="@+id/MarkAndModel" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent> </com.google.android.flexbox.FlexboxLayout> 我们初始化FlexboxLayout: flexboxLayout = view.findViewById(R.id.type); LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); layoutParams.setMargins(20,10,20,10); TextView textView = new TextView(flexboxLayout.getContext()); textView.setLayoutParams(layoutParams); textView.setText(data[i]); textView.setBackgroundResource(R.drawable.white_panel); textView.setTextColor(Color.BLACK); textView.setTextSize(16); textView.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD)); textView.setPadding(10,10,10,10); flexboxLayout.addView(textView);
您需要使用 FlexboxLayout。
将依赖项添加到 Gradle:
添加
FlexboxLayout
到布局:我们初始化
FlexboxLayout
: