激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語(yǔ)言|JavaScript|易語(yǔ)言|vb.net|

服務(wù)器之家 - 編程語(yǔ)言 - Android - Android自定義控件ViewGroup實(shí)現(xiàn)標(biāo)簽云

Android自定義控件ViewGroup實(shí)現(xiàn)標(biāo)簽云

2022-02-27 16:30Owen-Chan Android

這篇文章主要為大家詳細(xì)介紹了Android自定義控件ViewGroup實(shí)現(xiàn)標(biāo)簽云,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了Android自定義控件ViewGroup實(shí)現(xiàn)標(biāo)簽云的具體代碼,供大家參考,具體內(nèi)容如下

實(shí)現(xiàn)的功能:

Android自定義控件ViewGroup實(shí)現(xiàn)標(biāo)簽云

基本繪制流程:

構(gòu)造函數(shù)獲取自定義屬性
onMeasure()方法,測(cè)量子控件的大小
onLayout()方法,對(duì)子控件進(jìn)行布局

1、自定義屬性

?
1
2
3
4
5
6
7
8
<resources>
 
  <declare-styleable name="TabsViewGroup">
    <attr name="tabVerticalSpace" format="dimension" />
    <attr name="tabHorizontalSpace" format="dimension" />
  </declare-styleable>
 
</resources>

2、構(gòu)造函數(shù)中獲取自定義屬性值

?
1
2
3
4
5
6
7
8
9
public TabsViewGroup(Context context, AttributeSet attrs) {
    super(context, attrs);
    TypedArray attrArray = context.obtainStyledAttributes(attrs, R.styleable.TabsViewGroup);
    if (attrArray != null) {
      childHorizontalSpace = attrArray.getDimensionPixelSize(R.styleable.TabsViewGroup_tabHorizontalSpace, 0);
      childVerticalSpace = attrArray.getDimensionPixelSize(R.styleable.TabsViewGroup_tabHorizontalSpace, 0);
      attrArray.recycle();
    }
  }

3、onMeasure函數(shù)測(cè)量子控件大小,然后設(shè)置當(dāng)前控件大小

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
@Override
  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    int widthSize = MeasureSpec.getSize(widthMeasureSpec);
    int heightSize = MeasureSpec.getSize(heightMeasureSpec);
 
    int widthModel = MeasureSpec.getMode(widthMeasureSpec);
    int heightModel = MeasureSpec.getMode(heightMeasureSpec);
 
    //輸出寬度高度以及測(cè)量模式
    Log.i(TAG, "sizeWidth :" + widthSize);
    switch (widthModel) {
      case MeasureSpec.AT_MOST:
        Log.i(TAG, "Width model AT_MOST");
        break;
      case MeasureSpec.EXACTLY:
        Log.i(TAG, "Width model EXACTLY");
        break;
      case MeasureSpec.UNSPECIFIED:
        Log.i(TAG, "Width model UNSPECIFIED");
        break;
    }
 
    Log.i(TAG, "sizeHeight :" + heightSize);
    switch (heightModel) {
      case MeasureSpec.AT_MOST:
        Log.i(TAG, "Height model AT_MOST");
        break;
      case MeasureSpec.EXACTLY:
        Log.i(TAG, "Height model EXACTLY");
        break;
      case MeasureSpec.UNSPECIFIED:
        Log.i(TAG, "Height model UNSPECIFIED");
        break;
    }
    //左右邊距
    int left = getPaddingLeft();
    int top = getPaddingTop();
    //記錄每一行的寬度和高度
    int lineWidth = 0;
    int lineHeight = 0;
    // 如果是warp_content情況下,記錄寬和高
    int containerWidth = 0;
    int containerHeight = 0;
    // 遍歷每個(gè)子元素
    for (int index = 0; index < getChildCount(); index++) {
      View child = getChildAt(index);
      if (child.getVisibility() == GONE) {
        continue;
      }
      measureChild(child, widthMeasureSpec, heightMeasureSpec);
      MarginLayoutParams params = (MarginLayoutParams) child.getLayoutParams();
 
      int childWidth = child.getMeasuredWidth() + params.leftMargin + params.rightMargin + childHorizontalSpace;
      int childHeight = child.getMeasuredHeight() + params.topMargin + params.bottomMargin + childVerticalSpace;
 
      if (lineWidth + childWidth > widthSize - getPaddingRight() - getPaddingLeft()) {
        containerWidth = Math.max(lineWidth, childWidth);
        lineWidth = childWidth;
        containerHeight += lineHeight;
        lineHeight = childHeight;
        Rect rect = new Rect(left,
            containerHeight + top,
            left + childWidth - childHorizontalSpace,
            containerHeight + top + child.getMeasuredHeight());
        child.setTag(rect);
      } else {
        Rect rect = new Rect(lineWidth + left,
            containerHeight + top,
            lineWidth + left + childWidth - childHorizontalSpace,
            containerHeight + top + child.getMeasuredHeight());
        child.setTag(rect);
        lineWidth += childWidth;
        lineHeight = Math.max(lineHeight, childHeight);
      }
    }
 
    containerWidth = Math.max(containerWidth, lineWidth) + getPaddingLeft() + getPaddingRight();
    containerHeight += lineHeight + getPaddingTop() + getPaddingBottom();
    containerHeight -= childVerticalSpace;
    setMeasuredDimension(widthModel == MeasureSpec.EXACTLY ? widthSize : containerWidth,
        heightModel == MeasureSpec.EXACTLY ? heightSize : containerHeight);
}

4、onLayout函數(shù)對(duì)所有子控件重新布局

?
1
2
3
4
5
6
7
8
9
10
11
12
@Override
  protected void onLayout(boolean changed, int l, int t, int r, int b) {
    int childCount = getChildCount();
    for (int i = 0; i < childCount; i++) {
      View child = getChildAt(i);
      if (child.getVisibility() == GONE) {
        continue;
      }
      Rect location = (Rect) child.getTag();
      child.layout(location.left, location.top, location.right, location.bottom);
    }
}

使用方法

1、布局問(wèn)自己中直接引用

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:owen="http://schemas.android.com/apk/res-auto"
  android:id="@+id/activity_main"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:background="#ffffff"
  android:orientation="vertical">
 
  <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="10dp"
    android:textSize="16sp"
    android:text="國(guó)際足球"/>
 
  <View
    android:layout_width="wrap_content"
    android:layout_height="0.1dp"
    android:background="#e0e0e0"/>
 
  <com.ownchan.tabviewgroup.view.TabsViewGroup
    android:id="@+id/tabs_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="10dip"
    owen:tabHorizontalSpace="10dp"
    owen:tabVerticalSpace="10dp" />
 
</LinearLayout>

2、代碼添加標(biāo)簽

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
@Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    initView();
  }
 
  private void initView() {
    TabsViewGroup tabsViewGroup = (TabsViewGroup) findViewById(R.id.tabs_view);
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
        ViewGroup.LayoutParams.WRAP_CONTENT);
    String[] tabs = new String[]{"世預(yù)賽歐洲區(qū)",
        "世預(yù)賽南美區(qū)", "五人世界杯", "意甲", "美國(guó)大聯(lián)盟", "烏超", "英冠", "國(guó)際冠軍杯", "巴西圣保羅州杯", "巴西杯"};
    for (int i = 0; i < tabs.length; i++) {
      TextView textView = new TextView(this);
      textView.setText(tabs[i]);
      textView.setTextColor(Color.WHITE);
      textView.setBackgroundDrawable(getResources().getDrawable(R.drawable.tabs_bg));
      textView.setGravity(Gravity.CENTER);
      textView.setPadding(10, 0, 10, 0);
      textView.setTextSize(16);
      tabsViewGroup.addView(textView, params);
    }
}

效果圖:

Android自定義控件ViewGroup實(shí)現(xiàn)標(biāo)簽云

項(xiàng)目Github地址

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

原文鏈接:https://blog.csdn.net/owenchan1987/article/details/59480006

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 99精品视频免费看 | 午夜视频在线免费观看 | 青草伊人网 | 日本综合久久 | 国产资源视频在线观看 | 91高清在线免费观看 | 亚洲第一成人在线观看 | 色人久久| 久久精品国产99久久久古代 | 午夜久| 久久tv免费国产高清 | 亚洲成人激情av | 13一14毛片免费看 | 一本一道久久久a久久久精品91 | av在线免费观看网址 | 精品99在线视频 | hd极品free性xxx一护士 | 成人毛片av在线 | 色污视频在线观看 | 91九色免费视频 | 亚洲一区在线免费视频 | 最新av在线播放 | 久久久久久中文字幕 | 久久久久久久久久美女 | 人禽l交免费视频观看 视频 | 国av在线| 偿还的影视高清在线观看 | 久久国产综合视频 | 精品一区在线视频 | 狠狠色成色综合网 | 免费毛片在线视频 | 欧美 国产 亚洲 卡通 综合 | 免费观看高清视频网站 | 成人福利在线观看 | 中文欧美日韩 | 国产精品久久久久网站 | 污版视频在线观看 | 日韩毛片毛片久久精品 | 久久99久久99免费视频 | 欧美成年人视频在线观看 | 91久久综合 |