我正在循环中动态生成一些 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);
}
最佳答案
我建议将 recylerview 用于此类目的。它更有效和动态。示例在Kotlin
<?xml version="1.0" encoding="utf-8"?>
<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/consRideInfo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/10dp"
android:padding="@dimen/10dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/incActionBar">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rcvList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="0dp"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:itemCount="3"
tools:listitem="@layout/row_item" />
</androidx.constraintlayout.widget.ConstraintLayout>
在 Activity 上定义适配器
private var adapter: GeneralAdapter<DataModel, MainActivity>? = null
private val list: ArrayList<DataModel> = ArrayList()
并在 onCreate 中
adapter = GeneralAdapter(R.layout.row_item, list, this)
binding.rcvList.adapter = adapter
并创建用于显示的适配器
class GeneralAdapter<D, F>(
val layoutId: Int,
private val arrayList: ArrayList<D>,
val listener: F
) : RecyclerView.Adapter<GeneralAdapter.GeneralHolder<D, F>>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): GeneralHolder<D, F> {
val layoutInflater = LayoutInflater.from(parent.context)
val binding: ViewDataBinding =
DataBindingUtil.inflate(layoutInflater, layoutId, parent, false)
return GeneralHolder(binding, listener)
}
override fun getItemCount(): Int = arrayList.size
override fun onBindViewHolder(holder: GeneralHolder<D, F>, position: Int) {
holder.bind(arrayList[position])
}
class GeneralHolder<D, F>(
private val binding: ViewDataBinding,
val listener: F
) :
RecyclerView.ViewHolder(binding.root) {
fun bind(data: D) {
binding.setVariable(BR.item, data)
binding.setVariable(BR.presenter, listener)
binding.executePendingBindings()
}
}
}
关于android - 动态 TextView 显示不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70079324/