Android--根據子控制項的大小自動換行的ViewGroup

来源:http://www.cnblogs.com/819158327fan/archive/2016/02/26/5222039.html
-Advertisement-
Play Games

1、自定義ViewGroup 1 /** 2 * Created by Administrator on 2016/2/26. 3 * 4 * --------自動換行的ViewGroup----------- 5 */ 6 public class LineWrapLayout extends V


1、自定義ViewGroup

  1 /**
  2  * Created by Administrator on 2016/2/26.
  3  *
  4  * --------自動換行的ViewGroup-----------
  5  */
  6 public class LineWrapLayout extends ViewGroup {
  7     private static final boolean DEBUG = true;
  8     private static final String TAG = "AutoLineFeedLayout";
  9 
 10     /**
 11      * 左間距
 12      */
 13     private int paddingLeft = 10;
 14     /**
 15      * 右間距
 16      */
 17     private int paddingRight = 10;
 18     /**
 19      *
 20      */
 21     private int paddingTop = 10;
 22     /**
 23      *
 24      */
 25     private int paddingBottom = 10;
 26 
 27     /**
 28      * 水平方向間距
 29      */
 30     private int horizontalSpace = 10;
 31     /**
 32      * 行間距
 33      */
 34     private int verticalSpace = 5;
 35 
 36 
 37     private List<Integer> listX;
 38     private List<Integer> listY;
 39 
 40     public LineWrapLayout(Context context) {
 41         super(context);
 42 
 43     }
 44     public LineWrapLayout(Context context, AttributeSet attrs) {
 45         super(context, attrs);
 46         init(attrs);
 47     }
 48 
 49     public LineWrapLayout(Context context, AttributeSet attrs, int defStyle) {
 50         super(context, attrs, defStyle);
 51         init(attrs);
 52     }
 53 
 54 
 55     @Override
 56     protected void onLayout(boolean changed, int l, int t, int r, int b) {
 57         if(DEBUG) Log.d(TAG, "--- onLayout changed :" + changed + " l :" + l + ",t :" + t + ",r :" + r + ",b :" + b);
 58         int count = getChildCount();
 59         int width = getWidth();
 60         Log.i(TAG, "寬度 :"+width);
 61 
 62 
 63         int startOffsetX = paddingLeft;// 橫坐標開始
 64         int startOffsety = 0;//縱坐標開始
 65         int rowCount = 1;
 66 
 67         int preEndOffsetX = startOffsetX;
 68 
 69         for (int i = 0; i < count; i++) {
 70             final View childView = getChildAt(i);
 71 
 72             int w = childView.getMeasuredWidth();
 73             int h = childView.getMeasuredHeight();
 74 
 75             int x = listX.get(i);
 76             int y = listY.get(i);
 77 
 78             // 佈局子控制項
 79             childView.layout(x, y, x + w, y + h);
 80         }
 81     }
 82     @Override
 83     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
 84         if(DEBUG) Log.v(TAG, "--- onMeasure()");
 85 
 86         int count = getChildCount();
 87         int width = measureWidth(widthMeasureSpec);
 88         Log.i(TAG, "寬度 :"+width);
 89 
 90 
 91         int startOffsetX = paddingLeft;// 橫坐標開始
 92         int startOffsety = 0+paddingTop;//縱坐標開始
 93         int rowCount = 1;
 94 
 95         int preEndOffsetX = startOffsetX;
 96 
 97         listX.clear();
 98         listY.clear();
 99         for (int i = 0; i < count; i++) {
100             Log.v(TAG, "----");
101             final View childView = getChildAt(i);
102             // 設置子空間Child的寬高
103             childView.measure(0,0);
104             /* 獲取子控制項Child的寬高 */
105             int childWidth = childView.getMeasuredWidth();
106             int childHeight = childView.getMeasuredHeight();
107             Log.v(TAG, "childWidth :"+childWidth+" childHeight :"+childHeight);
108             preEndOffsetX = startOffsetX + childWidth /*+ CHILD_MARGIN*/;
109             //TODO [yaojian]margin屬性?
110             if (preEndOffsetX > width - paddingRight ) {
111                 if (startOffsetX > paddingLeft) {
112                     /* 換行  */
113                     startOffsetX = paddingLeft;
114                     startOffsety += childHeight+verticalSpace;
115                     rowCount++;
116                 }
117             }
118             Log.d(TAG, "measure child :"+startOffsetX+", "+startOffsety+", "+preEndOffsetX+", "+(startOffsety+childHeight));
119             listX.add(startOffsetX);
120             listY.add(startOffsety);
121 
122 //            childView.layout(startOffsetX, startOffsety, preEndOffsetX, startOffsety+childHeight);
123             startOffsetX = startOffsetX + childWidth + horizontalSpace;
124         }
125         int lastLineHeight = 0;
126         View lastChild = getChildAt(count-1);
127         if(null != lastChild){
128             lastLineHeight = lastChild.getMeasuredHeight();
129         }
130         setMeasuredDimension(measureWidth(widthMeasureSpec), startOffsety+lastLineHeight+paddingBottom);
131 //        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
132 
133         // 註意setMeasuredDimension和resolveSize的用法
134 //        setMeasuredDimension(resolveSize(measuredWidth, widthMeasureSpec),
135 //                resolveSize(top, heightMeasureSpec));
136     }
137 
138     private int measureWidth(int measureSpec) {
139         int specMode = MeasureSpec.getMode(measureSpec);
140         int specSize = MeasureSpec.getSize(measureSpec);
141 
142 
143         // Default size if no limits are specified.
144         int result = 400;
145 
146         if (specMode == MeasureSpec.AT_MOST) {
147             // Calculate the ideal size of your control
148             // within this maximum size.
149             // If your control fills the available space
150             // return the outer bound.
151             result = specSize;
152         } else if (specMode == MeasureSpec.EXACTLY) {
153             // If your control can fit within these bounds return that value.
154             result = specSize;
155         }
156         return result;
157     }
158 
159     private void init(AttributeSet attrs) {
160         TypedArray attrArray = getContext().obtainStyledAttributes(attrs, R.styleable.AutoLineFeedLayout);
161         int attrCount = attrArray.getIndexCount();
162         for (int i = 0; i < attrCount; i++) {
163             int attrId = attrArray.getIndex(i);
164             switch (attrId) {
165                 case R.styleable.AutoLineFeedLayout_horizontalSpacing:{
166                     float dimen = attrArray.getDimension(attrId, 0);
167                     horizontalSpace = (int) dimen;
168                 }
169                 break;
170                 case R.styleable.AutoLineFeedLayout_verticalSpacing:{
171                     float dimen = attrArray.getDimension(attrId, 0);
172                     verticalSpace = (int) dimen;
173                 }
174                 break;
175                 case R.styleable.AutoLineFeedLayout_paddingBottom:{
176                     float dimen = attrArray.getDimension(attrId, 0);
177                     paddingBottom = (int) dimen;
178                 }
179                 break;
180                 case R.styleable.AutoLineFeedLayout_paddingLeft:{
181                     float dimen = attrArray.getDimension(attrId, 0);
182                     paddingLeft = (int) dimen;
183                 }
184                 break;
185                 case R.styleable.AutoLineFeedLayout_paddingRight:{
186                     float dimen = attrArray.getDimension(attrId, 0);
187                     paddingRight = (int) dimen;
188                 }
189                 break;
190                 case R.styleable.AutoLineFeedLayout_paddingTop:{
191                     float dimen = attrArray.getDimension(attrId, 0);
192                     paddingTop = (int) dimen;
193                 }
194                 break;
195                 case R.styleable.AutoLineFeedLayout_debug:{
196 
197                 }
198                 break;
199 
200                 default:
201                     break;
202             }
203 
204         }
205 
206         listX = new ArrayList<Integer>();
207         listY = new ArrayList<Integer>();
208     }
209 }

2、有一部分自定義屬性可供使用 attrs.xml

    <declare-styleable name="AutoLineFeedLayout">
        <attr name="debug" format="boolean"></attr>

        <attr name="paddingLeft" format="reference|dimension"/>
        <attr name="paddingRight" format="reference|dimension"/>
        <attr name="paddingTop" format="reference|dimension"/>
        <attr name="paddingBottom" format="reference|dimension"/>

        <attr name="verticalSpacing" format="reference|dimension"/>
        <attr name="horizontalSpacing" format="reference|dimension"/>
    </declare-styleable>

3、在佈局文件中和普通ViewGroup使用相同

4、在Activity中動態添加View或者ViewGroup

for (int i = 0;i < myData.getList().length;i++){
                        View child = View.inflate(ZhuanTiActivity.this,R.layout.item_mygridview_layout,null);
                        TextView textView = (TextView) child.findViewById(R.id.tv_title_item);
                        textView.setText(myData.getList()[i].getName());
                        textView.setTextColor(Color.argb(255, colorR, colorG, colorB));
                        textView.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Log.d("aaa",textView.getText());
                            }
                        });
                        custom_index_zhuanTiActivity.addView(child);
                    }

 


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

-Advertisement-
Play Games
更多相關文章
  • 這是因為 app.use(function * (){ 語句中有一個 * ,這種方式被稱為generator functions ,一般寫作function *(){...} 的形式,在此類function 中可以支持ES6的一種yield概念。 為了保證這種新型的方法可以編譯通過,在運行node 
  • This application's application-identifier entitlement does not match that of the installed application. These values must match for an upgrade to be a
  • 第一步、首先在你項目中創建一個包存放支持下拉刷新和上拉載入的類: 第二步、需要把兩個動畫導入進來,實現180度旋轉與360度旋轉: 第三步、需要把支持的下拉與上拉顯示的隱藏載入佈局給導入進來 第四步、需要添加strings.xml與colors.xml文件的內容添加到項目裡面: strings.xm
  • 分類:C#、Android、VS2015; 創建日期:2016-02-27 一、簡介 這一節演示如何利用以非同步方式(async、await)訪問SQLite資料庫。 二、示例4運行截圖 下麵左圖為初始頁面,右圖為單擊【創建資料庫】按鈕後的結果。 下麵左圖為單擊【添加單行】按鈕的結果,右圖為單擊【添加...
  • 近期因為項目的需要,研究了一下Android文件下載進度顯示的功能實現,接下來就和大家一起分享學習一下,希望對廣大初學者有幫助。 先上效果圖: 上方的藍色進度條,會根據文件下載量的百分比進行載入,中部的文本控制項用來現在文件下載的百分比,最下方的ImageView用來展示下載好的文件,項目的目的就是動
  • 作者: "@gzdaijie" 本文為作者原創,轉載請註明出處:http://www.cnblogs.com/gzdaijie/p/5222191.html 1.寫在前面 Android提供了豐富的 函數,本文介紹最常用的8種對話框的使用方法,包括普通(包含提示消息和按鈕)、列表、單選、多選、等待、
  • 從啟動界面到主界面之後的效果如圖所示,採用的是v4包下的DrawerLayout, activity_main.xml文件如下: ...
  • ReactNative高級交流群 127482131 或訪問 http://blog.1ygowu.com ReactNative技術專題
一周排行
    -Advertisement-
    Play Games
  • 前言 本文介紹一款使用 C# 與 WPF 開發的音頻播放器,其界面簡潔大方,操作體驗流暢。該播放器支持多種音頻格式(如 MP4、WMA、OGG、FLAC 等),並具備標記、實時歌詞顯示等功能。 另外,還支持換膚及多語言(中英文)切換。核心音頻處理採用 FFmpeg 組件,獲得了廣泛認可,目前 Git ...
  • OAuth2.0授權驗證-gitee授權碼模式 本文主要介紹如何筆者自己是如何使用gitee提供的OAuth2.0協議完成授權驗證並登錄到自己的系統,完整模式如圖 1、創建應用 打開gitee個人中心->第三方應用->創建應用 創建應用後在我的應用界面,查看已創建應用的Client ID和Clien ...
  • 解決了這個問題:《winForm下,fastReport.net 從.net framework 升級到.net5遇到的錯誤“Operation is not supported on this platform.”》 本文內容轉載自:https://www.fcnsoft.com/Home/Sho ...
  • 國內文章 WPF 從裸 Win 32 的 WM_Pointer 消息獲取觸摸點繪製筆跡 https://www.cnblogs.com/lindexi/p/18390983 本文將告訴大家如何在 WPF 裡面,接收裸 Win 32 的 WM_Pointer 消息,從消息裡面獲取觸摸點信息,使用觸摸點 ...
  • 前言 給大家推薦一個專為新零售快消行業打造了一套高效的進銷存管理系統。 系統不僅具備強大的庫存管理功能,還集成了高性能的輕量級 POS 解決方案,確保頁面載入速度極快,提供良好的用戶體驗。 項目介紹 Dorisoy.POS 是一款基於 .NET 7 和 Angular 4 開發的新零售快消進銷存管理 ...
  • ABP CLI常用的代碼分享 一、確保環境配置正確 安裝.NET CLI: ABP CLI是基於.NET Core或.NET 5/6/7等更高版本構建的,因此首先需要在你的開發環境中安裝.NET CLI。這可以通過訪問Microsoft官網下載並安裝相應版本的.NET SDK來實現。 安裝ABP ...
  • 問題 問題是這樣的:第三方的webapi,需要先調用登陸介面獲取Cookie,訪問其它介面時攜帶Cookie信息。 但使用HttpClient類調用登陸介面,返回的Headers中沒有找到Cookie信息。 分析 首先,使用Postman測試該登陸介面,正常返回Cookie信息,說明是HttpCli ...
  • 國內文章 關於.NET在中國為什麼工資低的分析 https://www.cnblogs.com/thinkingmore/p/18406244 .NET在中國開發者的薪資偏低,主要因市場需求、技術棧選擇和企業文化等因素所致。歷史上,.NET曾因微軟的閉源策略發展受限,儘管後來推出了跨平臺的.NET ...
  • 在WPF開發應用中,動畫不僅可以引起用戶的註意與興趣,而且還使軟體更加便於使用。前面幾篇文章講解了畫筆(Brush),形狀(Shape),幾何圖形(Geometry),變換(Transform)等相關內容,今天繼續講解動畫相關內容和知識點,僅供學習分享使用,如有不足之處,還請指正。 ...
  • 什麼是委托? 委托可以說是把一個方法代入另一個方法執行,相當於指向函數的指針;事件就相當於保存委托的數組; 1.實例化委托的方式: 方式1:通過new創建實例: public delegate void ShowDelegate(); 或者 public delegate string ShowDe ...