android - ListView 隐藏了 EditText

标签 android

我是 Android 新手,正在阅读 Wrox 的专业 android 4 应用程序开发书籍。在本书的第 4 章中,它解释了如何修改现有的 TextView 。我面临的问题是我的应用程序中的 ListView 隐藏了编辑文本框。它是隐藏的(可以在背景中看到)但仍然有效,可以通过它向列表中添加更多内容。下面是我的主要 Activity xml 的代码

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<EditText
    android:id="@+id/myEditText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:contentDescription="@string/addItemContentDescription"
    android:hint="@string/addItemHint"
    />
<ListView
    android:id="@+id/myListView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>
</RelativeLayout>

和我的 todolist_item xml

<?xml version="1.0" encoding="utf-8"?>
<com.example.wroxexample.ToDoListItemView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" 
android:padding="10dp"
android:scrollbars="vertical"
android:textColor="@color/notepad_text"
android:fadingEdge="vertical"
/>

最佳答案

第一个选项是使用 LinearLayout而不是 RelativeLayout .

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<EditText
    android:id="@+id/myEditText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:contentDescription="@string/addItemContentDescription"
    android:hint="@string/addItemHint"
    />
<ListView
    android:id="@+id/myListView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>
</LinearLayout>

RelativeLayout 允许您相对于其他元素定位元素。

另一方面,LinearLayout 会将元素按照它们在 xml 文件中出现的顺序逐个放置。

您的第二个选择是保留您的 RelativeLayout 并将以下标记添加到您的 ListView:

android:layout_below="@id/myEditText"

这会将 ListView 定位在 EditText 的下方。

关于android - ListView 隐藏了 EditText,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11864225/

相关文章:

java - 使用自定义适配器将 HashMap 数据映射到 listView

android - 设置安卓 :value with a gradle properties on manifest

java - 如何访问私有(private)类中的公共(public)方法

android - 从 Android 应用程序发布 LinkedIn 消息

android - 为什么在卸载应用程序后保留 FirebaseAuth currentUser()?

Android Play 上传失败,错误代码 : The maximum SDK version cannot be lower than the minimum SDK version

android - HERE map 在 FrameLayout 上工作 popBackStack() 不工作

android - 删除数据库中创建的前 20 行

android - 我的 Android 应用程序泄漏,我应该检查什么?

android - 哪个Android手机支持FEATURE_AUDIO_PRO?