You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为什么Android模拟器中自定义的ListView疗法列表无法正常显示?

问题排查与解决方法

  1. 布局文件名拼写不匹配
    你Java代码中setContentView(R.layout.activity_stress)加载的布局名称为activity_stress,但你提供的布局文件命名为activty_stress.xml,少了一个字母i,导致代码加载的不是你编写的这个布局,自然无法显示列表。
    解决方法:将布局文件重命名为activity_stress.xml,或者将setContentView中的布局引用修改为你实际的文件名即可。

  2. 相对布局下ListView位置未明确绑定
    你使用RelativeLayout作为父容器,但没有给ListView指定和上方TextView的相对位置,仅靠写死的layout_marginTop=70dp调整位置,在不同分辨率、字体大小的设备上,可能出现TextView高度超过70dp,盖住ListView的情况。
    解决方法:给ListView添加android:layout_below="@id/textView4"属性,明确列表在标题下方展示,可适当调整marginTop的值减小间距。

  3. ListView高度设置异常
    你给ListView设置的高度为wrap_content,在部分场景下会触发测量异常,导致列表高度计算为0无法显示。
    解决方法:将ListView的高度改为match_parent,配合layout_below属性可以刚好占满标题下方的所有屏幕空间,显示效果更稳定。


修改后的完整布局代码

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
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:background="#83BCD4"
tools:context=".stress">

<TextView
    android:id="@+id/textView4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:text="Find your relaxation"
    android:textColor="@color/white"
    android:textSize="18pt" />

<ListView
    android:id="@+id/listview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@id/textView4"
    android:layout_marginTop="10dp"/>

 </RelativeLayout>

可选优化(不影响显示,仅消除警告)

Java代码逻辑本身没有影响显示的问题,建议给ArrayAdapter加上泛型声明避免类型擦除警告,修改为:

ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1,list);

内容的提问来源于stack exchange,提问作者Saint Blue

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.30 07:45:01