標籤:
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 設定自動匹配高度