gpt4 book ai didi

android - 动态 TextView 显示不正确

转载 作者:行者123 更新时间:2023-12-04 23:57:21 24 4
gpt4 key购买 nike

我正在循环中动态生成一些 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);
}

enter image description here

最佳答案

我建议将 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com