GridView嵌套在ScrollView里只有一行的問題

来源:http://www.cnblogs.com/libuyi/archive/2016/10/08/5937322.html
-Advertisement-
Play Games

遇到這個問題 網上找到的解決辦法: 方法一:就是上面說的通過計算出來ListView或者GridView中的子列高度和 進行顯示:public void setListViewHeightBasedOnChildren(ListView listView) { ListAdapter listAda ...


遇到這個問題

網上找到的解決辦法:

方法一:就是上面說的通過計算出來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" />

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 效果圖如下: ...
  • 效果圖: ...
  • 做App測試時監測使用期間的cpu,記憶體,流量,電量等指標時,發現的企鵝很好用的工具。 備份至此,方便後期查閱 以下內容摘抄自企鵝GT官網 http://code.tencent.com/gt.html http://gt.qq.com/ 文檔 http://gt.qq.com/docs.html ...
  • 開發第一應用 可以開發屬於自己的應用,是否有點小激動?好吧!讓我們開始,首先點擊Start a new Android Studio Project創建工程:接下來需要輸入應用名稱(第一個字母要大寫)、公司域以及指定應用存放目錄,點擊Next按鈕進入下一步: 如果第一個字母不是大寫,會提示:The ...
  • 遍歷可變數組的同時刪除數組元素 獲取系統當前語言 UITableView的Group樣式下頂部空白處理 UITableView的plain樣式下,取消區頭停滯效果 獲取某個view所在的控制器 兩種方法刪除NSUserDefaults所有記錄 列印系統所有已註冊的字體名稱 獲取圖片某一點的顏色 字元 ...
  • 最近半年以來,Android熱補丁技術熱潮繼續爆發,各大公司相繼推出自己的開源框架。Tinker在最近也順利完成了公司的審核,並非常榮幸的成為github.com/Tencent上第一個正式公開的項目。 ...
  • 蘋果在iOS10開放了siriKit介面給第三方應用。目前,QQ已經率先適配了Siri的發消息和打電話功能。這意味著在iOS10中你可以直接告訴Siri讓它幫你發QQ消息和打QQ電話了,聽起來是不是很酷炫? 那麼第三方應用使用Siri的體驗究竟如何?哪些應用可以接入SiriKit?接入SiriKi... ...
  • 原文: http://www.2cto.com/kf/201512/455888.html http://blog.csdn.net/yangqingqo/article/details/48371123 http://inthecheesefactory.com/blog/things-you-n ...
一周排行
    -Advertisement-
    Play Games
  • 示例項目結構 在 Visual Studio 中創建一個 WinForms 應用程式後,項目結構如下所示: MyWinFormsApp/ │ ├───Properties/ │ └───Settings.settings │ ├───bin/ │ ├───Debug/ │ └───Release/ ...
  • [STAThread] 特性用於需要與 COM 組件交互的應用程式,尤其是依賴單線程模型(如 Windows Forms 應用程式)的組件。在 STA 模式下,線程擁有自己的消息迴圈,這對於處理用戶界面和某些 COM 組件是必要的。 [STAThread] static void Main(stri ...
  • 在WinForm中使用全局異常捕獲處理 在WinForm應用程式中,全局異常捕獲是確保程式穩定性的關鍵。通過在Program類的Main方法中設置全局異常處理,可以有效地捕獲並處理未預見的異常,從而避免程式崩潰。 註冊全局異常事件 [STAThread] static void Main() { / ...
  • 前言 給大家推薦一款開源的 Winform 控制項庫,可以幫助我們開發更加美觀、漂亮的 WinForm 界面。 項目介紹 SunnyUI.NET 是一個基於 .NET Framework 4.0+、.NET 6、.NET 7 和 .NET 8 的 WinForm 開源控制項庫,同時也提供了工具類庫、擴展 ...
  • 說明 該文章是屬於OverallAuth2.0系列文章,每周更新一篇該系列文章(從0到1完成系統開發)。 該系統文章,我會儘量說的非常詳細,做到不管新手、老手都能看懂。 說明:OverallAuth2.0 是一個簡單、易懂、功能強大的許可權+可視化流程管理系統。 有興趣的朋友,請關註我吧(*^▽^*) ...
  • 一、下載安裝 1.下載git 必須先下載並安裝git,再TortoiseGit下載安裝 git安裝參考教程:https://blog.csdn.net/mukes/article/details/115693833 2.TortoiseGit下載與安裝 TortoiseGit,Git客戶端,32/6 ...
  • 前言 在項目開發過程中,理解數據結構和演算法如同掌握蓋房子的秘訣。演算法不僅能幫助我們編寫高效、優質的代碼,還能解決項目中遇到的各種難題。 給大家推薦一個支持C#的開源免費、新手友好的數據結構與演算法入門教程:Hello演算法。 項目介紹 《Hello Algo》是一本開源免費、新手友好的數據結構與演算法入門 ...
  • 1.生成單個Proto.bat內容 @rem Copyright 2016, Google Inc. @rem All rights reserved. @rem @rem Redistribution and use in source and binary forms, with or with ...
  • 一:背景 1. 講故事 前段時間有位朋友找到我,說他的窗體程式在客戶這邊出現了卡死,讓我幫忙看下怎麼回事?dump也生成了,既然有dump了那就上 windbg 分析吧。 二:WinDbg 分析 1. 為什麼會卡死 窗體程式的卡死,入口門檻很低,後續往下分析就不一定了,不管怎麼說先用 !clrsta ...
  • 前言 人工智慧時代,人臉識別技術已成為安全驗證、身份識別和用戶交互的關鍵工具。 給大家推薦一款.NET 開源提供了強大的人臉識別 API,工具不僅易於集成,還具備高效處理能力。 本文將介紹一款如何利用這些API,為我們的項目添加智能識別的亮點。 項目介紹 GitHub 上擁有 1.2k 星標的 C# ...