日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

自己定义ViewGroup控件(一)-----gt;流式布局进阶(一)

發布時間:2023/12/10 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 自己定义ViewGroup控件(一)-----gt;流式布局进阶(一) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

main.xml

<?

xml version="1.0" encoding="utf-8"?> <com.example.SimpleLayout.MyLinLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#ff00ff" tools:context=".MainActivity" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:background="#ff0000" android:text="第一個VIEW" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:background="#00ff00" android:text="第二個VIEW" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="30dp" android:background="#0000ff" android:text="第三個VIEW" /> </com.example.SimpleLayout.MyLinLayout>


MainActivity

package com.example.SimpleLayout;import android.app.Activity; import android.os.Bundle;public class MainActivity extends Activity {@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);} }
MyLinLayout

package com.example.SimpleLayout;import android.content.Context; import android.util.AttributeSet; import android.view.View; import android.view.ViewGroup;/*** onMeasure():測量自己的大小,自己的大小。為正式布局提供建議。(注意。僅僅是建議,至于用不用。要看onLayout);* onLayout():使用layout()函數對全部子控件布局。 onDraw():依據布局的位置畫圖;* */ public class MyLinLayout extends ViewGroup {/*** 首先是3個構造器*/public MyLinLayout(Context context) {super(context);}public MyLinLayout(Context context, AttributeSet attrs) {super(context, attrs);}public MyLinLayout(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);}/*** 此ViewGroup的寬高屬性 android:layout_width="match_parent"--EXACTLY(確定)* android:layout_height="wrap_content"--AT_MOST(不確定)* * 他們是父類傳遞過來給當前view的一個建議值,建議值。即想把當前view的尺寸設置為寬widthMeasureSpec,* 高heightMeasureSpec* * ②、EXACTLY(全然)。父元素決定自元素的確切大小,子元素將被限定在給定的邊界里而忽略它本身大小;* ③、AT_MOST(至多)。子元素至多達到指定大小的值。*/@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {super.onMeasure(widthMeasureSpec, heightMeasureSpec);// 寬度、高度int measureWidth = MeasureSpec.getSize(widthMeasureSpec);int measureHeight = MeasureSpec.getSize(heightMeasureSpec);// 測量模式int measureWidthMode = MeasureSpec.getMode(widthMeasureSpec);int measureHeightMode = MeasureSpec.getMode(heightMeasureSpec);// 初始化ViewGroup寬、高int viewGroupHeight = 0;int viewGroupWidth = 0;// 獲取viewGroup中的每一個孩子View,進行遍歷int count = getChildCount();for (int i = 0; i < count; i++) {// 依次獲取每一個孩子View對象View child = getChildAt(i);// 測量每一個孩子View,將父類的模式傳進去--點開看源代碼measureChild(child, widthMeasureSpec, heightMeasureSpec);int childHeight = child.getMeasuredHeight();int childWidth = child.getMeasuredWidth();// ViewGroup高度遞增viewGroupHeight += childHeight;// ViewGroup寬度取最大值viewGroupWidth = Math.max(childWidth, viewGroupWidth);}// ViewGroup的寬不須要測量直接"match_parent"--EXACTLY// 高是"wrap_content"--AT_MOST,須要累加得到高度/*** ②、EXACTLY(全然)。父元素決定自元素的確切大小,子元素將被限定在給定的邊界里而忽略它本身大小。* ③、AT_MOST(至多),子元素至多達到指定大小的值。*/setMeasuredDimension((measureWidthMode == MeasureSpec.EXACTLY) ? measureWidth: viewGroupWidth,(measureHeightMode == MeasureSpec.EXACTLY) ? measureHeight: viewGroupHeight);}/*** getMeasureWidth()方法在measure()過程結束后就能夠獲取到了。而getWidth()方法要在layout()* 過程結束后才干獲取到。再重申一遍*/@Overrideprotected void onLayout(boolean changed, int l, int t, int r, int b) {int top = 0;// 獲取子View的數量int count = getChildCount();for (int i = 0; i < count; i++) {// 依次獲取每一個孩子View對象View child = getChildAt(i);// 獲取孩子view的寬高int childHeight = child.getMeasuredHeight();int childWidth = child.getMeasuredWidth();child.layout(0, top, childWidth, top + childHeight);// 遞增孩子View的top值top += childHeight;}} }

轉載于:https://www.cnblogs.com/liguangsunls/p/7233445.html

總結

以上是生活随笔為你收集整理的自己定义ViewGroup控件(一)-----gt;流式布局进阶(一)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。