【发布时间】:2011-02-07 21:27:44
【问题描述】:
我刚刚开始在 Android 上开发,所以这可能是一个非常基本的事情,但是。 . .
我很难在LinearLayout 中设置ListView。可能是我的 XML 布局有问题,也可能是我的数据绑定代码有问题。
很多教程都说我的Activity 应该扩展ListActivity,这让我很困惑。为什么这个小部件需要更改我的整个 UI?
问题是,我只希望项目的滚动框占据这一屏幕的一部分。 ListView 是不是使用了错误的小部件?我应该改用ScrollView 吗?
谢谢——这是我的 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"
android:background="#FFF"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/enter_text"
/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
<EditText
android:id="@+id/txtTo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:layout_weight="1"
android:width="200px"/>
</LinearLayout>
<ListView android:id="@+id/listRecent"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
这是我的代码:
public class MyApp extends Activity {
private String TAG = "MyApp";
//UI ELEMENTS
EditText txtTo;
ListView listRecent;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//GET REFERENCES TO UI ELEMENTS
listRecent = (ListView) this.findViewById(R.id.listRecent);
try {
//----------------------------------------
// create the grid item mapping
String[] from = new String[] {"zero", "one", "two", "three"};
int[] to = new int[] {0, 1, 2, 3};
// prepare the list of all records
List<HashMap<String, String>> fillMaps = new ArrayList<HashMap<String, String>>();
for(int i = 0; i < 10; i++){
HashMap<String, String> map = new HashMap<String, String>();
map.put("rowid", "" + i);
map.put("col_1", "col_1_item_" + i);
map.put("col_2", "col_2_item_" + i);
map.put("col_3", "col_3_item_" + i);
fillMaps.add(map);
}
// fill in the grid_item layout
SimpleAdapter adapter = new SimpleAdapter(this, fillMaps, R.layout.recently_dingdonged, from, to);
listRecent.setAdapter(adapter);
//----------------------------------------
}
catch(Exception e) {
Log.e(TAG, "Error here (3116019)", e);
}
}
【问题讨论】: