国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

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

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

服務(wù)器之家 - 編程語言 - 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、布局問自己中直接引用

?
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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 久久只有精品 | а天堂中文最新一区二区三区 | 亚洲精品一区二区在线观看 | 91.成人天堂一区 | 日韩免费在线视频 | 亚洲伦理一区二区 | 成人午夜小视频 | 国产97色在线 | 亚洲 | 精品国产乱码久久久久久1区2区 | 特黄一级 | jizzxxx日本 | 黄色av大片在线观看 | 欧美国产日韩一区二区三区 | 久久av一区二区 | 国产1区 | 日韩在线观看中文字幕 | 精品久久网 | 久久久精品影院 | 少妇精品久久久久久久久久 | 国产区一区 | 国产在线资源 | 国产黄a一级 | 欧美色综合天天久久综合精品 | 日本在线免费 | 性欧美精品久久久久久久 | 亚洲一区二区三区蜜桃 | 国产成人在线电影 | 国产精品久久久久久福利一牛影视 | 91网在线 | 日韩久久久久久 | 亚洲国产一区二区三区精品 | 日韩欧美高清 | 日本精品一区二区三区在线观看视频 | 欧美激情精品久久久久久黑人 | 青草免费视频 | 天天精品 | 欧美激情国产日韩精品一区18 | 在线成人 | 国产日韩欧美在线观看 | 日韩精品一区二区在线观看 | 一区在线观看 |