【发布时间】:2022-01-01 20:29:20
【问题描述】:
我正在循环中动态生成一些 TextView。它们正在显示,但我无法在它们之间制造差距。它们显示为一行而不是单独的行。
<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout
android:id="@+id/ll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="18dp"
android:paddingRight="18dp"
android:paddingTop="60dp"
android:paddingBottom="@dimen/activity_vertical_margin"
android:focusableInTouchMode="true"
android:orientation="vertical">
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
tviewlayout.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/light"
android:textAlignment="gravity"
android:padding="10dp"
android:layout_margin="10dp"
android:textSize="20sp" />
Java 代码
LinearLayout linearLayout = (LinearLayout) binding.ll;
for (int i=0; i<arrSplit.length; i++)
{
TextView tv = (TextView)getLayoutInflater().inflate(R.layout.tviewlayout, null);
tv.setText(arrSplit[i]);
tv.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
linearLayout.addView(tv);
}
【问题讨论】:
-
如果你将任何东西放入一个约束布局(在本例中是你的 LinearLayout),你需要定义它的约束。将 LL 的所有 4 个约束设置为“父”,事情应该会更好。还将 LL 的
layout_width和layout_height设置为“0dp”。这告诉 Android 它应该使用给定的约束。 -
移除约束布局!
-
你能提供一张你得到的照片吗
-
@Zain 我添加了一张照片
-
那么,我认为每个训练行都是
LinearLayout中的TextView?如果是这样,这是意料之中的......你能展示你期望看到的东西吗?所以不会有混乱