java - Android "ListView"项目文本重叠并被切断

标签 java android xml listview user-interface

我正在尝试将按钮添加到项目的 ListView 中,以便用户可以通过单击按钮/复选框将特价商品添加到他们的“Collection 夹”中。在添加此功能之前,列表的布局看起来不错。

这是“之前”的图像:Good Layout

这是“之前”的java代码:

ArrayList<Special> specials = specialda.getAllSpecialsByDay(selectedDay);
ListView listView = (ListView) findViewById(R.id.listView);
ArrayAdapter<Special> adapter = new ArrayAdapter<Special>(this, android.R.layout.simple_list_item_1, specials);
listView.setAdapter(adapter);

现在,将 Java 代码更改为:

ArrayList<Special> specials = specialda.getAllSpecialsByDay(selectedDay);
ListView listView = (ListView) findViewById(R.id.listView);
        ArrayAdapter<Special> adapter = new ArrayAdapter<Special>(this, android.R.layout.simple_list_item_multiple_choice, specials);
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
listView.setAdapter(adapter);

我最终得到了这个扭曲的观点:

After image

这是 xml 布局文件:

<LinearLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.yourfullname.flashcards.laxspecials.DisplaySpecials"
    tools:showIn="@layout/activity_display_specials">

    <ListView
         android:id="@+id/listView"
         android:layout_width="wrap_content"
         android:layout_height="match_parent"
         android:layout_alignParentTop="true"
         android:layout_centerHorizontal="true" />
 </LinearLayout>

关于我做错了什么或需要改变的任何提示?

谢谢

编辑:这是 display_specials_button.xml 文件:

<LinearLayout 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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/listView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?android:attr/listPreferredItemHeightSmall"
    android:textAppearance="?android:attr/textAppearanceListItemSmall"
    android:gravity="center_vertical"
    android:checkMark="?android:attr/listChoiceIndicatorMultiple"
    android:paddingStart="?android:attr/listPreferredItemPaddingStart"
    android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
    android:paddingLeft="?android:attr/listPreferredItemPaddingLeft"
    android:paddingRight="?android:attr/listPreferredItemPaddingRight"/>

</LinearLayout>

最佳答案

您可以复制 android.R.layout.simple_list_item_multiple_choice 的源代码来执行必要的更改:

<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?android:attr/listPreferredItemHeightSmall"
    android:textAppearance="?android:attr/textAppearanceListItemSmall"
    android:gravity="center_vertical"
    android:checkMark="?android:attr/listChoiceIndicatorMultiple"
    android:paddingStart="?android:attr/listPreferredItemPaddingStart"
    android:paddingEnd="?android:attr/listPreferredItemPaddingEnd" />

不同之处在于,现在您以不同的方式提供 android:minHeightandroid:layout_height

关于java - Android "ListView"项目文本重叠并被切断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43397954/

相关文章:

java - 如何在运行时设置 JTextField 的宽度?

java - Selenium div 属性不断变化,我怎样才能找到这个元素?

java - 如何用不同的笔触和填充颜色绘制文本?

java - 如何通过连接到远程位置来更新 Android 数据库

java - 解析 xml ...我认为这个问题。是关于编码的

Java - 用于存储函数的类型安全异构容器模式

java - 有没有更有效的方法在 for 循环中创建新变量?

android - 探戈的新手。缺少图书馆

php - 在 php 中创建具有空值的 XML 节点

xml - 是 &amp;有效的