最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android自定義ViewGroup之第一次接觸ViewGroup

 更新時(shí)間:2016年06月16日 09:11:13   作者:可樂淘  
這篇文章主要為大家詳細(xì)介紹了Android自定義ViewGroup之第一次接觸ViewGroup,感興趣的小伙伴們可以參考一下

整理總結(jié)自鴻洋的博客:http://blog.csdn.net/lmj623565791/article/details/38339817/
 一、com.cctvjiatao.customviewgroup.act.MainActivity.Java
 需求:我們定義一個(gè)ViewGroup,內(nèi)部可以傳入0到4個(gè)childView,分別依次顯示在左上角,右上角,左下角,右下角

public class MainActivity extends AppCompatActivity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
//  setContentView(R.layout.activity_main2);
//  setContentView(R.layout.activity_main3);
 }
}

二、com.cctvjiatao.customviewgroup.view.CustomViewGroup.java
 一)、ViewGroup是是什么?作用呢?
1、它相當(dāng)于放置View的容器。xml布局文件中,凡是以“l(fā)ayout”開頭的屬性都是和ViewGroup(即容器)相關(guān)的,比如高度(layout_height)、寬度(layout_width)、對(duì)齊方式(layout_gravity)等;
2、它給childView計(jì)算出建議的寬、高和測(cè)量模式,決定childView的位置;
為什么是“建議的寬、高”而不是直接確定呢?因?yàn)楫?dāng)childView的寬、高設(shè)置為wrap_content時(shí),只有childView自己才能計(jì)算出自己的寬和高。
 二)、View的作用是什么?
1、它根據(jù)測(cè)量模式和ViewGroup給出的建議的寬、高,計(jì)算出自己的寬、高;
2、在ViewGroup為其指定的區(qū)域內(nèi)繪制自己的形態(tài);
三)、View的三種測(cè)量模式
1、EXACTLY:表示設(shè)置了精確的值,一般當(dāng)childView設(shè)置其寬高為精確值、match_parent時(shí),ViewGroup會(huì)將其設(shè)置為EXACTLY;
2、AT_MOST:表示子布局被限制在一個(gè)最大值內(nèi),一般當(dāng)childView設(shè)置其寬、高為wrap_content時(shí),ViewGroup會(huì)將其設(shè)置為AT_MOST;
3、UNSPECIFIED:表示子布局想要多大就多大,一般出現(xiàn)在AadapterView的item的heightMode中、ScrollView的childView的heightMode中;此種模式比較少見。
四)、ViewGroup 和 LayoutParams的關(guān)系
當(dāng)在LinearLayout中寫childView的時(shí)候,可以寫layout_gravity,layout_weight屬性;
而在RelativeLayout中的childView有l(wèi)ayout_centerInParent屬性,卻沒有l(wèi)ayout_gravity,layout_weight,這是為什么呢?
這是因?yàn)槊總€(gè)ViewGroup需要指定一個(gè)LayoutParams,用于確定支持childView支持哪些屬性,比如LinearLayout指定LinearLayout.LayoutParams等
如果去看LinearLayout的源碼,會(huì)發(fā)現(xiàn)其內(nèi)部定義了LinearLayout.LayoutParams,在此類中,你可以發(fā)現(xiàn)weight和gravity的身影。
五)、從API角度分析ViewGroup和View的作用
View 根據(jù) ViewGroup 傳入的測(cè)量值和測(cè)量模式,確定自己的寬、高(在onMeasure中完成),然后在onDraw中完成對(duì)自己的繪制;
ViewGroup需要給View傳入View的測(cè)量值和測(cè)量模式(在onMeasure中完成),而且對(duì)于此ViewGroup的父布局,ViewGroup也要在onMeasure中完成對(duì)自己寬、高的確定;
ViewGroup需要再onLayout中完成對(duì)其childView的位置的指定。

public class CustomViewGroup extends ViewGroup {

 public CustomViewGroup(Context context) {
  super(context);
 }

 public CustomViewGroup(Context context, AttributeSet attrs) {
  super(context, attrs);
 }

 public CustomViewGroup(Context context, AttributeSet attrs, int defStyleAttr) {
  super(context, attrs, defStyleAttr);
 }

 /**
  * 一、重寫generateLayoutParams,確定該ViewGroup的LayoutParams
  * 返回MarginLayoutParams的實(shí)例,這樣就為我們的ViewGroup指定了其LayoutParams為MarginLayoutParams
  */
 @Override
 public LayoutParams generateLayoutParams(AttributeSet attrs) {
  return new MarginLayoutParams(getContext(), attrs);
 }

 /**
  * 二、計(jì)算所有ChildView的寬度和高度 然后根據(jù)ChildView的計(jì)算結(jié)果,設(shè)置自己的寬和高
  */
 @Override
 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  //1、獲得此ViewGroup上級(jí)容器為其推薦的寬和高,以及計(jì)算模式
  int widthMode = MeasureSpec.getMode(widthMeasureSpec);
  int heightMode = MeasureSpec.getMode(heightMeasureSpec);
  int sizeWidth = MeasureSpec.getSize(widthMeasureSpec);
  int sizeHeight = MeasureSpec.getSize(heightMeasureSpec);
  // 2、計(jì)算出所有的childView的寬和高
  measureChildren(widthMeasureSpec, heightMeasureSpec);
  // 3、如果ViewGroup布局是wrap_content時(shí),根據(jù)childView的尺寸,計(jì)算容器的寬和高
  int width = 0;//ViewGroup的寬度
  int height = 0;//ViewGroup的高度
  int cCount = getChildCount();//childView的數(shù)量
  int cWidth = 0;//childView的總寬度
  int cHeight = 0;//childView的總高度
  MarginLayoutParams cParams = null;//View的測(cè)量模式
  int lHeight = 0;// 用于計(jì)算左邊兩個(gè)childView的高度
  int rHeight = 0;// 用于計(jì)算右邊兩個(gè)childView的高度,最終高度取二者之間大值
  int tWidth = 0;// 用于計(jì)算上邊兩個(gè)childView的寬度
  int bWidth = 0;// 用于計(jì)算下面兩個(gè)childiew的寬度,最終寬度取二者之間大值
  for (int i = 0; i < cCount; i++) {
   View childView = getChildAt(i);
   cWidth = childView.getMeasuredWidth();
   cHeight = childView.getMeasuredHeight();
   cParams = (MarginLayoutParams) childView.getLayoutParams();
   if (i == 0 || i == 1) {// 上面兩個(gè)childView
    tWidth += cWidth + cParams.leftMargin + cParams.rightMargin;
   }
   if (i == 2 || i == 3) {// 下面兩個(gè)childView
    bWidth += cWidth + cParams.leftMargin + cParams.rightMargin;
   }
   if (i == 0 || i == 2) {// 左面兩個(gè)childView
    lHeight += cHeight + cParams.topMargin + cParams.bottomMargin;
   }
   if (i == 1 || i == 3) {// 右面兩個(gè)childView
    rHeight += cHeight + cParams.topMargin + cParams.bottomMargin;
   }
  }
  width = Math.max(tWidth, bWidth);//取最大寬度
  height = Math.max(lHeight, rHeight);//去最大高度
  //4、如果是wrap_content設(shè)置為我們計(jì)算的值;否則直接設(shè)置為父容器計(jì)算的值
  setMeasuredDimension(
    (widthMode == MeasureSpec.EXACTLY) ? sizeWidth : width,
    (heightMode == MeasureSpec.EXACTLY) ? sizeHeight : height
  );
 }

 /**
  * 三、重寫onLayout,對(duì)其所有childView進(jìn)行定位(設(shè)置childView的繪制區(qū)域)
  */
 @Override
 protected void onLayout(boolean changed, int l, int t, int r, int b) {
  int cCount = getChildCount();
  int cWidth = 0;
  int cHeight = 0;
  MarginLayoutParams cParams = null;
  //遍歷所有childView根據(jù)其寬和高,以及margin進(jìn)行布局
  for (int i = 0; i < cCount; i++) {
   View childView = getChildAt(i);
   cWidth = childView.getMeasuredWidth();
   cHeight = childView.getMeasuredHeight();
   cParams = (MarginLayoutParams) childView.getLayoutParams();
   int cl = 0, ct = 0, cr = 0, cb = 0;
   switch (i) {
    case 0:
     cl = cParams.leftMargin;
     ct = cParams.topMargin;
     break;
    case 1:
     cl = getWidth() - cWidth - cParams.leftMargin - cParams.rightMargin;
     ct = cParams.topMargin;
     break;
    case 2:
     cl = cParams.leftMargin;
     ct = getHeight() - cHeight - cParams.bottomMargin;
     break;
    case 3:
     cl = getWidth() - cWidth - cParams.leftMargin - cParams.rightMargin;
     ct = getHeight() - cHeight - cParams.bottomMargin;
     break;
   }
   cr = cl + cWidth;
   cb = cHeight + ct;
   childView.layout(cl, ct, cr, cb);
  }
 }
}

三、三種布局
 activity_main.xml

<com.cctvjiatao.customviewgroup.view.CustomViewGroup xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="200dp"
 android:layout_height="200dp"
 android:background="#AA333333">

 <TextView
  android:layout_width="50dp"
  android:layout_height="50dp"
  android:background="#FF4444"
  android:gravity="center"
  android:text="0"
  android:textColor="#FFFFFF"
  android:textSize="22sp"
  android:textStyle="bold" />

 <TextView
  android:layout_width="50dp"
  android:layout_height="50dp"
  android:background="#00ff00"
  android:gravity="center"
  android:text="1"
  android:textColor="#FFFFFF"
  android:textSize="22sp"
  android:textStyle="bold" />

 <TextView
  android:layout_width="50dp"
  android:layout_height="50dp"
  android:background="#ff0000"
  android:gravity="center"
  android:text="2"
  android:textColor="#FFFFFF"
  android:textSize="22sp"
  android:textStyle="bold" />

 <TextView
  android:layout_width="50dp"
  android:layout_height="50dp"
  android:background="#0000ff"
  android:gravity="center"
  android:text="3"
  android:textColor="#FFFFFF"
  android:textSize="22sp"
  android:textStyle="bold" />

</com.cctvjiatao.customviewgroup.view.CustomViewGroup>

activity_main2.xml

<com.cctvjiatao.customviewgroup.view.CustomViewGroup xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:background="#AA333333">

 <TextView
  android:layout_width="150dp"
  android:layout_height="150dp"
  android:background="#E5ED05"
  android:gravity="center"
  android:text="0"
  android:textColor="#FFFFFF"
  android:textSize="22sp"
  android:textStyle="bold" />

 <TextView
  android:layout_width="50dp"
  android:layout_height="50dp"
  android:background="#00ff00"
  android:gravity="center"
  android:text="1"
  android:textColor="#FFFFFF"
  android:textSize="22sp"
  android:textStyle="bold" />

 <TextView
  android:layout_width="50dp"
  android:layout_height="50dp"
  android:background="#ff0000"
  android:gravity="center"
  android:text="2"
  android:textColor="#FFFFFF"
  android:textSize="22sp"
  android:textStyle="bold" />

 <TextView
  android:layout_width="50dp"
  android:layout_height="50dp"
  android:background="#0000ff"
  android:gravity="center"
  android:text="3"
  android:textColor="#FFFFFF"
  android:textSize="22sp"
  android:textStyle="bold" />

</com.cctvjiatao.customviewgroup.view.CustomViewGroup>

activity_main3.xml

<com.cctvjiatao.customviewgroup.view.CustomViewGroup xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:background="#AA333333">

 <TextView
  android:layout_width="150dp"
  android:layout_height="150dp"
  android:background="#E5ED05"
  android:gravity="center"
  android:text="0"
  android:textColor="#FFFFFF"
  android:textSize="22sp"
  android:textStyle="bold" />

 <TextView
  android:layout_width="50dp"
  android:layout_height="50dp"
  android:background="#00ff00"
  android:gravity="center"
  android:text="1"
  android:textColor="#FFFFFF"
  android:textSize="22sp"
  android:textStyle="bold" />

 <TextView
  android:layout_width="50dp"
  android:layout_height="50dp"
  android:background="#ff0000"
  android:gravity="center"
  android:text="2"
  android:textColor="#FFFFFF"
  android:textSize="22sp"
  android:textStyle="bold" />

 <TextView
  android:layout_width="150dp"
  android:layout_height="150dp"
  android:background="#0000ff"
  android:gravity="center"
  android:text="3"
  android:textColor="#FFFFFF"
  android:textSize="22sp"
  android:textStyle="bold" />

</com.cctvjiatao.customviewgroup.view.CustomViewGroup>

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Flutter實(shí)現(xiàn)文本滾動(dòng)高亮效果的示例講解

    Flutter實(shí)現(xiàn)文本滾動(dòng)高亮效果的示例講解

    這篇文章主要介紹了如何利用Flutter時(shí)時(shí)渲染頁面從而達(dá)到文本滾動(dòng)高亮的效果,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下
    2022-03-03
  • Android StickListView實(shí)現(xiàn)懸停效果

    Android StickListView實(shí)現(xiàn)懸停效果

    這篇文章主要介紹了Android StickListView實(shí)現(xiàn)懸停效果的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-06-06
  • Android中Viewpager禁止滑動(dòng)的實(shí)現(xiàn)

    Android中Viewpager禁止滑動(dòng)的實(shí)現(xiàn)

    有時(shí)候在開發(fā)中會(huì)遇到一些特別的要求,如在ViewPager中嵌入ListView,或者再嵌入一個(gè)ViewPager,那么在滑動(dòng)的時(shí)候就會(huì)造成被嵌入的XXView不能滑動(dòng)了,那么就把最外層的ViewPager禁止滑動(dòng)吧,本文就介紹了Android中Viewpager禁止滑動(dòng)的實(shí)現(xiàn)方法,需要的朋友可以參考。
    2017-05-05
  • Android使用Notification實(shí)現(xiàn)通知功能

    Android使用Notification實(shí)現(xiàn)通知功能

    這篇文章主要為大家詳細(xì)介紹了Android使用Notification實(shí)現(xiàn)通知功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-11-11
  • Android 自繪控件

    Android 自繪控件

    Android中自定義控件分為三種:1.自繪控件 2.組合控件 3.繼承控件。本篇介紹下自繪控件。下面跟著小編一起來看下吧
    2017-02-02
  • OpenGL ES透視投影實(shí)現(xiàn)方法(四)

    OpenGL ES透視投影實(shí)現(xiàn)方法(四)

    這篇文章主要為大家詳細(xì)介紹了OpenGL ES透視投影的實(shí)現(xiàn)方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-05-05
  • android 布局之ConstraintLayout的使用

    android 布局之ConstraintLayout的使用

    這篇文章主要介紹了android 布局之ConstraintLayout的使用,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-04-04
  • Android 列表形式的切換的示例代碼

    Android 列表形式的切換的示例代碼

    本篇文章主要介紹了Android 列表形式的切換的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-09-09
  • 利用kotlin實(shí)現(xiàn)一個(gè)餅圖實(shí)例代碼

    利用kotlin實(shí)現(xiàn)一個(gè)餅圖實(shí)例代碼

    餅狀圖是以不同顏色的圓的切片表示的值。下面這篇文章主要給大家介紹了關(guān)于利用kotlin實(shí)現(xiàn)一個(gè)餅圖的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-12-12
  • Android自定義帶有圓形進(jìn)度條的可長(zhǎng)按控件功能

    Android自定義帶有圓形進(jìn)度條的可長(zhǎng)按控件功能

    這篇文章主要介紹了Android自定義帶有圓形進(jìn)度條的可長(zhǎng)按控件,思路很簡(jiǎn)單,使用簡(jiǎn)單的畫筆工具就可以完成這個(gè)控件,本文結(jié)合示例代碼給大家介紹的非常詳細(xì),需要的朋友參考下吧
    2022-06-06

最新評(píng)論

外汇| 东兴市| 望江县| 旅游| 乌兰浩特市| 凤山县| 丽江市| 宁德市| 南康市| 新营市| 隆昌县| 乌鲁木齐县| 龙南县| 玉林市| 广河县| 都匀市| 永吉县| 盱眙县| 巩义市| 瑞昌市| 泰州市| 东乡县| 睢宁县| 昌乐县| 永定县| 泌阳县| 卢湾区| 保德县| 社旗县| 铜陵市| 文安县| 奉新县| 通道| 陵川县| 乌兰县| 南康市| 锦州市| 东平县| 黄骅市| 龙陵县| 阳山县|