我正在循环中动态生成一些TextViews,它们可以被显示,但是我无法在它们之间创建间隔。它们显示为一行而不是分开的行。
<?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);
}
layout_width
和layout_height
设置为“0dp”。这告诉Android它应该使用给定的约束条件。 - muetzenfloLinearLayout
中的TextView
,我认为是这样的?如果是这样的话,那就是预期的了。你能展示一下你期望看到的吗,这样就不会有混淆了。 - Zain