我刚刚开始在Android上进行开发,因此这可能是非常基本的事情。 。 。

我很难在ListView中设置LinearLayout。可能是我的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);
    }

}

最佳答案

您无需将Activity扩展为ListActivity。您可以,但这不是必需的。

关于您的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">

        <EditText
            android:id="@+id/txtTo"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:gravity="left"
            android:layout_weight="0.5"
            android:width="200px"/>
       </LinearLayout>
       <ListView android:id="@+id/listRecent"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.5" />
  </LinearLayout>

08-18 10:28