編輯:關於Android編程
一.使用網上用的動態改變listview高度的方法,該方法只適用於item布局是LinearLayout布局的情況,不能是其他的,因為其他的Layout(如RelativeLayout)沒有重寫onMeasure(),所以會在onMeasure()時拋出異常。所以使用限制較大。
public class Utility {
public static void setListViewHeightBasedOnChildren(ListView listView) {
//獲取ListView對應的Adapter
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
// pre-condition
return;
}
int totalHeight = 0;
for (int i = 0, len = listAdapter.getCount(); i < len; i++) { //listAdapter.getCount()返回數據項的數目
View listItem = listAdapter.getView(i, null, listView);
listItem.measure(0, 0); //計算子項View 的寬高
totalHeight += listItem.getMeasuredHeight(); //統計所有子項的總高度
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
//listView.getDividerHeight()獲取子項間分隔符占用的高度
//params.height最後得到整個ListView完整顯示需要的高度
listView.setLayoutParams(params);
}
}
二.網上有帖子說在ScrollView中添加一屬性 android:fillViewport="true" ,這樣就可以讓ListView全屏顯示了。在我機器上測試失敗了。
三:重寫ListView、gridView(推薦)
重寫ListView:
public class MyListView extends ListView {
public MyListView(Context context) {
// TODO Auto-generated method stub
super(context);
}
public MyListView(Context context, AttributeSet attrs) {
// TODO Auto-generated method stub
super(context, attrs);
}
public MyListView(Context context, AttributeSet attrs, int defStyle) {
// TODO Auto-generated method stub
super(context, attrs, defStyle);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// TODO Auto-generated method stub
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
}
}
重寫GridView:
/**
*自定義gridview,解決ScrollView中嵌套gridview顯示不正常的問題(1行)
*/
public class MyGridView extends GridView{
public MyGridView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyGridView(Context context) {
super(context);
}
public MyGridView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
}
}
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持本站。
Android之——獲取進程總數、內存、任務列表
如今,基於Android平台的應用越來越多,不乏這樣一些應用程序,這些應用可以多手機中的進程,內存和任務列表進行管理。Android系統中沒有自帶的應用程序對這些數據進行
android http——OkHttp使用詳解
okhttp已經被google認定為推薦的android請求框架,我們也可以在as中直接加入:okhttp的依賴庫是:compile 'com.squareup.
Android本地化存儲Cookie(針對HttpClient)
因為最近有人問我怎麼保存HttpClient的Cookie, 所以這裡寫下, 順便記錄總結吧. 當然, 有Android網絡編程經歷的童鞋一看就懂喇~ 就不多說
Android仿XListView支持下拉刷新和上劃加載更多的自定義RecyclerView
首先給大家展示下效果圖,感覺還不錯,請繼續往下閱讀:下拉刷新: 上劃加載 &nbs