Android 中 GridView嵌套在ScrollView里只有一行的解決方法
在做android項目中遇到一個bug,GridView嵌套在ScrollView里只有一行的問題。下面小編在網(wǎng)上找到了解決方法,具體方法如下所示:
方法一:就是上面說的通過計算出來ListView或者GridView中的子列高度和 進行顯示:
public void setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
return;
}
int totalHeight = 0;
for (int i = 0; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, listView);
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
((MarginLayoutParams)params).setMargins(15, 15, 15, 15);
listView.setLayoutParams(params);
}
方法二:重寫GridView和ListView的onMeasure方法,直接給它一個足夠大的高度:
重寫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:
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);
}
}
xml中的布局:
<com.xxx.MyGridView android:id="@+id/mygridview" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:horizontalSpacing="5dp" android:numColumns="4" android:stretchMode="columnWidth" android:verticalSpacing="6dp" />
以上所述是小編給大家介紹的Android 中 GridView嵌套在ScrollView里只有一行的解決方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
- Android ScrollView 下嵌套 ListView 或 GridView出現(xiàn)問題解決辦法
- Android中ScrollView嵌套GridView顯示不全解決方法
- Android中ScrollView嵌套GridView的解決辦法
- Android 中ScrollView嵌套GridView,ListView的實例
- Android開發(fā)之機頂盒上gridview和ScrollView的使用詳解
- Android編程開發(fā)之ScrollView嵌套GridView的方法
- Android之ScrollView嵌套ListView和GridView沖突的解決方法
- ScrollView嵌套ListView及ListView嵌套的高度計算方法
- Android開發(fā)實現(xiàn)ScrollView中嵌套兩個ListView的方法
- Android解決ScrollView下嵌套ListView和GridView中內容顯示不全的問題
相關文章
Android中關于CoordinatorLayout的一些實用布局技巧
大家都知道CoordinatorLayout是一個“加強版”的 FrameLayout,那么下面這篇文章主要給大家分享了Android中關于CoordinatorLayout的一些布局技巧,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考借鑒,下面來一起看看吧。2017-06-06
Android Studio開發(fā)環(huán)境搭建教程詳解
android studio是最近比較火的開發(fā),那么android studio開發(fā)環(huán)境怎么搭建呢?下面通過本文給大家記錄下Android Studio開發(fā)環(huán)境搭建教程詳解,需要的朋友參考下吧2017-11-11
解析Android開發(fā)優(yōu)化之:從代碼角度進行優(yōu)化的技巧
下面我們就從幾個方面來了解Android開發(fā)過程中的代碼優(yōu)化,需要的朋友參考下2013-05-05
Android在WebView中調用系統(tǒng)下載的方法
這篇文章主要為大家詳細介紹了Android在WebView中調用系統(tǒng)下載的簡單使用,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-05-05

