ListView包含不包含在同一行的EditText的TextView和ListView

时间:2015-07-11 18:31:21

标签: java android android-listview android-edittext textview

因此,图片将让您更好地了解问题:

enter image description here

请参阅,我向右TextView添加了3 ListView,向右EditText添加了3 ListView,但它们不在同一行。 那么我该如何解决呢?

我的问题与XML有关:

 <LinearLayout
            android:id="@+id/inner_linear_layout_id"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_below="@id/date_picker_id"
            android:baselineAligned="false"
            android:orientation="horizontal"
            android:weightSum="1" >

        <ListView
            android:id="@+id/dates_list_view_id"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.5" 
            />

        <ListView
            android:id="@+id/comments_list_view_id"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.5" 
            />
    </LinearLayout>

我与Java问题有关:

arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,datesListView.getId());
datesListView.setAdapter(arrayAdapter);
commentsListView.setAdapter(arrayAdapter);


date.setTextSize(20);
datesListView.addFooterView(date);
commentText.setId(998);
commentText.setInputType(EditorInfo.TYPE_CLASS_TEXT);       
commentText.setHint("Add a comment");
commentText.setTextSize(15);

commentsListView.addFooterView(commentText); 

dateTextViewcommentTextEditText

1 个答案:

答案 0 :(得分:1)

好的,这是一个解决方案。当然还有许多其他可能的解决方案。

这是列表项的XML。它描述了列表中单个条目的视图。这里使用TextView和EditText构建:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="horizontal"
              android:layout_width="match_parent"
              android:layout_height="wrap_content">

    <TextView
            android:id="@+id/text_datetime"
            android:text="8:20p.m."
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

    <EditText
            android:id="@+id/edit_comment"
            android:text="no comment"
            android:layout_weight="1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

</LinearLayout>

这是主要活动的XML,它实际上包含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"
        >
    <ListView
            android:id="@+id/my_list"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            />
</LinearLayout>

这就是MainActivity的代码,其中包含一些绑定到ListView的演示数据。这实际上将ListView与其项目以及ListView项目的相应XML连接起来:

package com.example.myapp;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import java.text.SimpleDateFormat;
import java.util.*;

public class MyActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        ListView list = (ListView)findViewById(R.id.my_list);
        if (list != null) {
            final List<DemoItem> demoList = new ArrayList<DemoItem>();

            final SimpleDateFormat hourDateFormat = new SimpleDateFormat("HH:mm", Locale.getDefault());
            final Calendar c = Calendar.getInstance();
            final Date now = c.getTime();
            final String now_ = hourDateFormat.format(now);

            demoList.add(0, new DemoItem(now_, "first comment"));
            demoList.add(1, new DemoItem(now_, "second comment"));
            demoList.add(2, new DemoItem(now_, "third comment"));

            DemoAdapter adapter = new DemoAdapter(this, demoList);
            list.setAdapter(adapter);
        }
    }

    class DemoItem {
        public String datetime;
        public String comment;

        public DemoItem(String datetime, String comment) {
            this.datetime = datetime;
            this.comment = comment;
        }
    }

    class DemoAdapter extends ArrayAdapter<DemoItem> {

        public DemoAdapter(Context context, List<DemoItem> items) {
            super(context, R.layout.list_item, items);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            if (convertView == null)
                convertView = getLayoutInflater().inflate(R.layout.list_item, null);

            DemoItem item = super.getItem(position);

            EditText editComment = (EditText)convertView.findViewById(R.id.edit_comment);
            if (editComment != null)
                editComment.setText(item.comment);

            TextView txtDatetime = (TextView)convertView.findViewById(R.id.text_datetime);
            if (txtDatetime != null)
                txtDatetime.setText(item.datetime);

            return convertView;
        }
    }
}

这就是它最终的样子:

looks like that

如果您不使用ListView,那么这是一个相同的RelativeLayout。但它不是动态的,也不是绑定到任何适配器:

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

    <TextView
            android:id="@+id/text_datetime1"
            android:text="8:20p.m."
            android:layout_toLeftOf="@+id/edit_comment1"
            android:layout_alignBaseline="@+id/edit_comment1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"/>

    <EditText
            android:id="@+id/edit_comment1"
            android:text="no comment"
            android:layout_alignParentRight="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

    <TextView
            android:id="@+id/text_datetime2"
            android:text="8:20p.m."
            android:layout_toLeftOf="@+id/edit_comment2"
            android:layout_alignBaseline="@+id/edit_comment2"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"/>

    <EditText
            android:id="@+id/edit_comment2"
            android:text="no comment"
            android:layout_alignParentRight="true"
            android:layout_below="@+id/edit_comment1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

    <TextView
            android:id="@+id/text_datetime3"
            android:text="8:20p.m."
            android:layout_toLeftOf="@+id/edit_comment3"
            android:layout_alignBaseline="@+id/edit_comment3"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"/>

    <EditText
            android:id="@+id/edit_comment3"
            android:text="no comment"
            android:layout_alignParentRight="true"
            android:layout_below="@+id/edit_comment2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

</RelativeLayout>