【问题标题】:Customize ListView自定义列表视图
【发布时间】:2023-04-06 20:42:01
【问题描述】:

我正在处理 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.listview 替换android.R.layout.simple_list_item_1,但正如我所预料的那样,它不起作用。

我尝试创建在教程中找到的其他一些自定义列表视图,但是在测试过滤器时,它会关闭应用程序。

我只需要更改文本的大小并将其居中。

这是我的原始代码:

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) {

        }
    })

【问题讨论】:

    标签: android android-layout listview kotlin


    【解决方案1】:

    android.R.*android 包中 R 类的导入。将其替换为 R.layout.listview.xmlcom.your.package.R.layout.listview.xml,具体取决于您的导入。

    【讨论】:

    • 感谢您的帮助,我按照您的建议做了,它只是关闭了我的应用程序。而不是以正确的布局加载列表
    • 查找堆栈跟踪
    猜你喜欢
    • 2011-07-25
    • 2015-12-22
    • 2018-05-10
    • 2020-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多