我正在使用 ListView:
我只想在我的列表视图的设计中简单地编辑一些东西,比如改变文本的大小、居中等等。
我已经读过,因为我使用的是“SIMPLE_LIST_ITEM_1”,所以我不能像我想要的那样编辑它。
我进行了一项研究,发现我需要创建自己想要使用的列表布局。所以我做到了,并创建了一个名为 listview.xml 的布局,如下所示:
<android.support.constraint.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:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="367dp"
android:layout_height="142dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0">
<TextView
android:id="@+id/txtvlist1"
android:layout_width="263dp"
android:layout_height="108dp"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:layout_marginEnd="9dp"
android:gravity="top|center" />
</RelativeLayout>
</android.support.constraint.ConstraintLayout>
因此,一旦我定义了我想要使用的布局,我就尝试用 替换它android.R.layout.simple_list_item_1
,android.R.layout.listview
但正如我所期望的那样,它不起作用。
我尝试创建在教程中找到的其他一些自定义列表视图,但是在测试过滤器时,它会关闭应用程序。
我所需要的只是改变文本的大小并将其居中。
这是我的原始代码:
val animalsnames = arrayOf("cat","dog", "mouse", "parrot", "lion", "panda")
internal lateinit var animalsadapter: ArrayAdapter<String>
val list = findViewById(R.id.list1) as ListView
animalsadapter = ArrayAdapter(
this@MainActivity,
android.R.layout.simple_list_item_1,
animalsnames)
list.adapter = animalsadapter
这是选定位置项的条件:
list.setOnItemClickListener { adapterView, view, i, l ->
when (animalsnames.indexOfFirst { it == animalsadapter.getItem(i) }) {
0 -> {
webView.loadUrl("https://cat.com")
telefono.text = "phonecat"
webView2.loadUrl("http://cat2.com")
}
1 -> {
webView.loadUrl("http://dog.com")
telefono.text = "phonedog"
webView2.loadUrl("https://dog2.com")
}
2 -> {
webView.loadUrl("https://mouse.com")
telefono.text = "mousephone"
webView2.loadUrl("http://mouse2.com")
}
这是我用作过滤器的编辑技术:
var et_search = findViewById(R.id.e_buscar) as EditText
et_search.addTextChangedListener(object : TextWatcher {
override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
animalsadapter.filter.filter(s)
}
override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {
}
override fun afterTextChanged(s: Editable) {
}
})