有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

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

enter image description here

请参见,我在左侧添加了3TextView,在右侧添加了3ListView,但它们不在同一行。 那我怎么解决呢

我的问题与XML有关:

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

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

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

我的问题与Java有关:

arrayAdapter = new ArrayAdapter<String>(this, 安卓.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); 

date是一个TextView,而commentText是一个EditText


共 (1) 个答案

  1. # 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>