【问题标题】:How to make a list of textviews?如何制作文本视图列表?
【发布时间】:2013-06-24 09:53:39
【问题描述】:
基本上这里是我想要完成的一个 mspaint 演示:
我不希望用户可以看到大纲,这只是为了让您了解我想要做什么。每当从 GUI 底部的 edittext-button 组合添加新目标时,我希望新的文本视图在彼此下方占据位置。
我知道如何保存数据 - SQLite。但我不知道如何使新的文本视图位于最后一个的底部 - 如果屏幕上没有更多空间,我希望该文本视图部分可滚动。
我不知道我需要用什么来完成这个。
提前致谢!
【问题讨论】:
标签:
android
android-listview
textview
【解决方案1】:
创建两个 xml 文件。
一种由列表视图组成的
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@drawable/bkgrnd_320x285">
<ListView
android:id="@+id/List1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:divider="#000000"
android:dividerHeight="1dp"
android:fastScrollEnabled="true"/>
</LinearLayout>
和另一个仅用于 textView 的 xml,例如。行.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Display Name"
android:textSize="20dp"
android:layout_marginTop="20dp" />
</LinearLayout>
然后在主要活动中你需要引用两个xml文件,如下所示
ListView ll = (ListView) findViewById(R.id.List1);
ArrayAdapter arrayAdapter = new ArrayAdapter(this,R.layout.row,R.id.textView2, arraylist_you_want_to_display);
ll.setAdapter(arrayAdapter);
其中R.layout.row是由textView组成的row.xml
而R.id.textView2是row.xml中textView的id
【解决方案2】:
您可以创建一个 LinearLayout,将方向设置为垂直。
添加一个ScrollView或者ListView显示上半部分,android:weight=1
将EditText添加到LinearLayout中,这样就完成了。
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/List1"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:weight="1"/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>