【问题标题】:How can I set up an inline ListView?如何设置内联 ListView?
【发布时间】: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);
    }

}

【问题讨论】:

    标签: android android-listview


    【解决方案1】:

    ListView 应该嵌套在滚动视图中。通过扩展 listActivity,您可以获得 setAdapter 并通过主对象而不是通过实例化列表将数据填充到列表视图中。不过,这绝不是要求。

    【讨论】:

      【解决方案2】:

      您无需将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>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-08-27
        • 2011-12-28
        • 2016-03-19
        • 1970-01-01
        • 2019-02-12
        • 2023-03-07
        • 2020-02-26
        相关资源
        最近更新 更多