android 中 listview 設定自動匹配高度

來源:互聯網
上載者:User

標籤:

1.布局檔案

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"            android:layout_width="match_parent"            android:layout_height="match_parent">    <LinearLayout        android:layout_width="match_parent"        android:layout_height="match_parent"        android:orientation="vertical">        <ListView            android:id="@+id/lv0"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:background="#ff0"            android:dividerHeight="5dp"/>        <TextView            android:layout_width="match_parent"            android:layout_height="50dp"            android:background="#0f0"            android:gravity="bottom"/>        <ListView            android:id="@+id/lv1"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:background="#f0f"            android:dividerHeight="5dp"/>    </LinearLayout></ScrollView>

2. java 代碼

import android.app.Activity;import android.os.Bundle;import android.util.Log;import android.view.View;import android.view.ViewGroup;import android.view.Window;import android.widget.ArrayAdapter;import android.widget.ListView;import java.util.ArrayList;import java.util.List;/** * 設定ListView的自動高度 * 3種方式: * 1.onWindowFocusChanged() 中設定 * 2.onResume() 中設定 * 3.onCreate() 中view.post()方法中設定 */public class ListViewAutoHeight extends Activity {    private static final String TAG = "autoHeight";    private ListView lv0;    private ListView lv1;    @Override    public void onWindowFocusChanged(boolean hasFocus) {        super.onWindowFocusChanged(hasFocus);//        setListViewAutoHeight(lv0);//        setListViewAutoHeight(lv1);        Log.e(TAG, "onWindowFocusChanged ");    }    @Override    protected void onResume() {        super.onResume();//        setListViewAutoHeight(lv0);//        setListViewAutoHeight(lv1);        Log.e(TAG, "onResume ");    }    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        requestWindowFeature(Window.FEATURE_NO_TITLE);        setContentView(R.layout.activity_listview_auto_height);        lv0 = (ListView) findViewById(R.id.lv0);        List<String> lv0Datas = new ArrayList<>();        for (int i = 0; i < 10; i++) {            lv0Datas.add("item_0_" + i);        }        ArrayAdapter<String> adapter0 = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, lv0Datas);        lv0.setAdapter(adapter0);        lv1 = (ListView) findViewById(R.id.lv1);        List<String> lv1Datas = new ArrayList<>();        for (int i = 0; i < 20; i++) {            lv1Datas.add("item_1_" + i);        }        ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, lv1Datas);        lv1.setAdapter(adapter1);        // 隨便在哪個控制項身上的post 方法中調用設定自動高度的方法即可        // post 是將該方法添加到Loop隊列的末尾,如果直接調用是不起作用的        lv1.post(new Runnable() {            @Override            public void run() {                setListViewAutoHeight(lv0);                setListViewAutoHeight(lv1);                Log.e(TAG, "post ");            }        });    }    /** 計算高度 */    private void setListViewAutoHeight(ListView lv) {        if (lv == null || lv.getCount() <= 0) return;        int totalHeight = 0;        for (int i = 0; i < lv.getCount(); i++) {            View view = lv.getAdapter().getView(i, null, lv);            view.measure(0, 0);            totalHeight += view.getMeasuredHeight();        }        ViewGroup.LayoutParams params = lv.getLayoutParams();        params.height = totalHeight + lv.getDividerHeight() * (lv.getCount() - 1) + lv.getPaddingTop() + lv.getPaddingBottom();        lv.setLayoutParams(params);    }}

 

android 中 listview 設定自動匹配高度

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.