android - EditText 不会显示在 ListView 上方

标签 android listview hidden android-edittext

我有一个 ListView Activity ,我希望在其上方显示一个 EditText(最终还有一个按钮)。

我相信我的 xml 没问题,但出于某种原因,EditText 没有显示。 ListView 占据了整个屏幕:(

这是我的 XML 的样子:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <EditText
        android:id="@+id/newitemtext"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:hint="@string/add_new_item"
    android:maxLines="1"
    />
    <ListView
        android:id="@+id/android:list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
    />
    <TextView
        android:id="@+id/android:empty"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/inbox_no_items"
    />
</LinearLayout>

有什么想法吗?

注意:在Eclipse 的xml 文件的布局选项卡中,EditText 显示。当我在模拟器中运行我的应用程序时,它没有。

谢谢你的时间

更新:

这是我在 onCreate 方法中设置内容 View 的地方

m_aryNewListItems = new ArrayList<MyListItem>();
m_Adapter = new MyListAdapter(this, R.layout.layout_list, m_aryNewListItems);
setListAdapter(m_Adapter);

这是我扩展的 ArrayAdapter:

private class MyListAdapter extends ArrayAdapter<MyListItem> {

// items in the list
private ArrayList<MyListItem> m_items;

// constructor
public MyListAdapter(Context context, int textViewResourceId, ArrayList<MyListItem> items) {
        super(context, textViewResourceId, items);
        this.m_items = items;
}

// get specific list item
public MyListItem getItem(int position) {
    if (m_items.size() > position) {
        return m_items.get(position);
    }
    else {
        return null;
    }
}

/*
 * Update screen display
 */
@Override
public View getView(int position, View convertView, ViewGroup parent) {

    // get the View for this list item
    View v = convertView;
    if (v == null) {
        LayoutInflater vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = vi.inflate(R.layout.layout_list_item, null);
    }

    // get the next MyListItem object
    MyListItem lstItem = m_items.get(position);

    // set up the list item
    if (lstItem != null) {
        TextView txtItem = (TextView) v.findViewById(R.id.list_itemname);
        TextView txtQty = (TextView) v.findViewById(R.id.list_itemqty);
        TextView txtPlace = (TextView) v.findViewById(R.id.list_place);

        // set item text
        if (txtItem != null) {
            txtItem.setText(lstItem.getItemName());
        }

        // set qty text
        String strQtyText = "Qty: ";
        if (txtQty != null && lstItem.getItemQty() != -1) {
            BigDecimal bd = new BigDecimal(lstItem.getItemQty());
            strQtyText += bd.stripTrailingZeros().toString();
            if(lstItem.getItemQtyUnitId() != -1) {
                strQtyText += " " + lstItem.getItemQtyUnitTextAbbrev();
            }
        }
        else {
            strQtyText += "--";
        }
        txtQty.setText(strQtyText);

        // set place text
        if (txtPlace != null && lstItem.getPlaceName() != null) {
            txtPlace.setText(lstItem.getPlaceName());
        }
    }

    // return the created view
    return v;
}

最佳答案

添加一个 android:layout_weight="1" 到 ListView xml

所以,这个东西:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:gravity="bottom"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">

        <EditText android:id="@+id/filter"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:singleLine="true"
            android:inputType="textAutoComplete"
            android:layout_weight="1"
            />

        <Button android:id="@+id/sort"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/sort"
            />

        <Button android:id="@+id/add_all"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/add_all"
            />

        <Button android:id="@+id/add"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/add"
            />
    </LinearLayout>

    <GridView android:id="@+id/myGrid"
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent"
        android:layout_weight="1"

        android:verticalSpacing="15dp"
        android:horizontalSpacing="10dp"

        android:numColumns="auto_fit"
        android:columnWidth="60dp"

        android:gravity="center"
        />

</LinearLayout>

产生这个:

alt text

关于android - EditText 不会显示在 ListView 上方,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3354241/

相关文章:

listview - React Native ListView 组件自动运行 onEndReached 函数,尽管我的滚动仍然在顶部

.net - WinForms ListView : How to specify width of a ListViewItem when there are columns but in View. 列表显示模式?

javascript - 无论父级可见性如何计算隐藏元素

android - 错误 : Execution failed for task ': app: processDebugGoogleServices

android - 如何在android中阻止安全模式

Android Studio Gradle OutofMemory 错误

linux - 使用 g++ 的符号可见性

android - Android 中用于通知的 XML 垂直线

android - 如何在android顶部的 ListView 中添加新项目

jquery - 使用 JQuery 中的 anchor 从当前页面和其他页面链接到特定(隐藏)选项卡?