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

Android自定義View之組合控件實現(xiàn)類似電商app頂部欄

 更新時間:2016年05月25日 09:46:36   作者:Brioal  
這篇文章主要為大家詳細(xì)介紹了Android自定義View之組合控件,實現(xiàn)類似電商app頂部欄的相關(guān)資料,具有參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了Android自定義View之組合控件,仿電商app頂部欄的相關(guān)代碼,供大家參考,具體內(nèi)容如下

效果圖:

這里寫圖片描述

分析:左右兩邊可以是TextView和Button,設(shè)置drawableTop即可,中間的看著像是EditText,但是用過淘寶天貓等類似app的話會發(fā)現(xiàn)點(diǎn)擊搜索不是在當(dāng)前Activit進(jìn)行搜索的,是跳轉(zhuǎn)到另外的頁面進(jìn)行的,所以用TextView然后設(shè)置背景即可. 實現(xiàn)流程

這里寫圖片描述

參數(shù)列表:

這里寫圖片描述

設(shè)置屬性文件:values下建立attrs.xml文件,添加需要自定義的屬性.

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <declare-styleable name="TopBar">
    <attr name="left_text" format="string" />// 左邊文字
    <attr name="right_text" format="string" /> // 右邊文字
    <attr name="center_text" format="string" />// 中間文字
    <attr name="side_text_size" format="dimension" />//邊界按鈕大小
    <attr name="center_text_size" format="dimension" />//中間文字大小
    <attr name="text_color" format="color" />//文字顏色
    <attr name="back_color" format="color" />//背景顏色
    <attr name="left_icon" format="reference" />//左邊的icon
    <attr name="right_icon" format="reference" />//右邊的icon
    <attr name="center_icon" format="reference" />//中間的icon
  </declare-styleable>
</resources>

代碼中獲取布局文件中設(shè)置的屬性:

TypedArray array = getContext().obtainStyledAttributes(attrs, R.styleable.TopBar);
    mLeftText = array.getString(R.styleable.TopBar_left_text);
    mRightText = array.getString(R.styleable.TopBar_right_text);
    mCenterText = array.getString(R.styleable.TopBar_center_text);
    side_text_size = (int) array.getDimension(R.styleable.TopBar_side_text_size, 10);
    center_text_size = (int) array.getDimension(R.styleable.TopBar_center_text_size, 15);
    mLeft_icon = array.getDrawable(R.styleable.TopBar_left_icon);
    mRight_icon = array.getDrawable(R.styleable.TopBar_right_icon);
    mCenter_icon = array.getDrawable(R.styleable.TopBar_center_icon);
    text_color = array.getColor(R.styleable.TopBar_text_color, getResources().getColor(R.color.colorAccent));
    back_color = array.getColor(R.styleable.TopBar_back_color, getResources().getColor(R.color.colorPrimary));
    array.recycle();

設(shè)置背景顏色:

setBackgroundColor(back_color);

添加按鈕:

//設(shè)置內(nèi)容
mLeftButton = new Button(getContext());//創(chuàng)建按鈕
mLeftButton.setText(mLeftText);//設(shè)置文字
mLeftButton.setTextSize(side_text_size);//設(shè)置文字大小
mLeftButton.setTextColor(text_color);//設(shè)置文字顏色
mLeftButton.setBackgroundColor(Color.TRANSPARENT);//設(shè)置按鈕的背景為透明
LayoutParams leftParams = new LayoutParams(80, 150);//設(shè)置布局
mLeft_icon.setBounds(0, 0, 55, 55); //設(shè)置icon的大小
mLeftButton.setCompoundDrawables(null, mLeft_icon, null, null); //添加icon
mLeftButton.setGravity(Gravity.CENTER);//設(shè)置置中
addView(mLeftButton, leftParams);//添加按鈕
//右按鈕類似,就不加注釋了
mRightButton = new Button(getContext());
mRightButton.setText(mRightText);
mRightButton.setTextSize(side_text_size);
mRightButton.setTextColor(text_color);
mRightButton.setBackgroundColor(Color.TRANSPARENT);
mRight_icon.setBounds(0, 0, 55, 55);
LayoutParams rightParams = new LayoutParams(80, 150);
mRightButton.setCompoundDrawables(null, mRight_icon, null, null);
mRightButton.setGravity(Gravity.CENTER);
addView(mRightButton, rightParams);

添加中間的TextView:(布局的排列是按添加順序的,所以中間TextVIew的添加應(yīng)該是在兩個按鈕中間的):

mCenterTextView = new TextView(getContext());//初始化TextView
mCenterTextView.setText(mCenterText);//設(shè)置文字
mCenterTextView.setTextSize(center_text_size);//設(shè)置文字大小
mCenterTextView.setTextColor(text_color);//設(shè)置文字顏色
mCenterTextView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);//設(shè)置文字靠左
mCenter_icon.setBounds(0, 0, 50, 50);//設(shè)置icon大小
mCenterTextView.setCompoundDrawables(mCenter_icon, null, null, null);//添加icon
LayoutParams params = new LayoutParams(0, 70);//創(chuàng)建布局屬性  mCenterTextView.setBackground(getResources().getDrawable(R.drawable.bg_search));//設(shè)置背景
params.weight = 1;//設(shè)置權(quán)重
params.gravity = Gravity.CENTER;//設(shè)置居中
params.setMargins(10, 0, 10, 0);//設(shè)置邊界
addView(mCenterTextView, params);//添加

處理高度的wrap_content屬性:

重寫onMeasure屬性,對wrap_content設(shè)置一個指定值

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    int specWidth = MeasureSpec.getSize(widthMeasureSpec);//獲取寬度
    int specHeight = MeasureSpec.getSize(heightMeasureSpec);//獲取高度
    int heightMode = MeasureSpec.getMode(heightMeasureSpec);//獲取高度的測量模式

    int height = 0;//初始化要設(shè)置的高度
    if (heightMode == MeasureSpec.EXACTLY) {//如果是確定的值,包括match_parent
      height = specHeight; //最終的值即為測量值
    } else {
      height = 120; //如果不是確定的值就設(shè)置為指定的高度
      if (heightMode == MeasureSpec.AT_MOST) {//如果是wrap_content就取測量值和指定值得最小值作為最終的值
        height = Math.min(specHeight, 120);
      }
    }
    setMeasuredDimension(specWidth, height);//設(shè)置寬高屬性
  }

添加點(diǎn)擊事件:

需要自定義一個回調(diào)

public interface onClick {
    void onLeftButtonClick();

    void onCenterButtonClick();

    void onRightButtonClick();
  }

創(chuàng)建一個回調(diào)并創(chuàng)建setX方法

private onClick onClick;

  public void setOnClick(TopBar.onClick onClick) {
    this.onClick = onClick;
  }

在添加按鈕的時候添加點(diǎn)擊事件

mLeftButton.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
        if (onClick != null) {
          onClick.onLeftButtonClick();
        }
      }
    });
mCenterTextView.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
        if (onClick != null) {
          onClick.onCenterButtonClick();
        }
      }
    });
 mRightButton.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
        if (onClick != null) {
          onClick.onRightButtonClick();
        }
      }
    });

至此自定義的組合控件就完成了,下面貼一下使用的方法:

布局文件:

  <com.brioa.diyviewtest.view.TopBar xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/topBar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    app:center_icon="@mipmap/ic_search"
    app:center_text="輸入關(guān)鍵字檢索"
    app:center_text_size="10sp"
    app:left_icon="@mipmap/ic_scan"
    app:left_text="掃一掃"
    app:right_icon="@mipmap/ic_msg"
    app:right_text="消息"
    app:side_text_size="6sp"
    app:text_color="#ffff">

  </com.brioa.diyviewtest.view.TopBar>

代碼設(shè)置:

mTopBar = (TopBar) findViewById(R.id.topBar);
    mTopBar.setOnClick(new TopBar.onClick() {
      @Override
      public void onLeftButtonClick() {
        Toast.makeText(mContext, "LeftClick", Toast.LENGTH_SHORT).show();
      }

      @Override
      public void onCenterButtonClick() {
        Toast.makeText(mContext, "CenterClick", Toast.LENGTH_SHORT).show();

      }

      @Override
      public void onRightButtonClick() {
        Toast.makeText(mContext, "RightClick", Toast.LENGTH_SHORT).show();

      }
    });

最終效果:

這里寫圖片描述

以上就是本文的全部內(nèi)容,希望對大家學(xué)習(xí)Android軟件編程有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 詳解Android Studio Git分支實踐

    詳解Android Studio Git分支實踐

    這篇文章主要介紹了Android Studio Git分支實踐,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-05-05
  • Android Studio 3.1.X中導(dǎo)入項目的正確方法分享

    Android Studio 3.1.X中導(dǎo)入項目的正確方法分享

    這篇文章主要給大家介紹了關(guān)于Android Studio 3.1.X中導(dǎo)入項目的正確方法,文中一步步將解決的方法以及可能遇到的問題介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-07-07
  • Android中使用Intent在Activity之間傳遞對象(使用Serializable或者Parcelable)的方法

    Android中使用Intent在Activity之間傳遞對象(使用Serializable或者Parcelable)的

    這篇文章主要介紹了 Android中使用Intent在Activity之間傳遞對象(使用Serializable或者Parcelable)的方法的相關(guān)資料,需要的朋友可以參考下
    2016-01-01
  • ANDROID中使用VIEWFLIPPER類實現(xiàn)屏幕切換(關(guān)于坐標(biāo)軸的問題已補(bǔ)充更改)

    ANDROID中使用VIEWFLIPPER類實現(xiàn)屏幕切換(關(guān)于坐標(biāo)軸的問題已補(bǔ)充更改)

    本篇文章主要介紹了ANDROID中使用VIEWFLIPPER類實現(xiàn)屏幕切換,具有一定的參考價值,有興趣的可以了解一下。
    2016-11-11
  • android點(diǎn)擊無效驗證的解決方法

    android點(diǎn)擊無效驗證的解決方法

    這篇文章主要給大家介紹了關(guān)于android點(diǎn)擊無效驗證的解決方法,文中通過示例代碼介紹的非常詳細(xì),對各位android開發(fā)者們具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-12-12
  • Android中backgroundDimEnabled的作用

    Android中backgroundDimEnabled的作用

    這篇文章主要介紹了Android中backgroundDimEnabled的作用的相關(guān)資料,希望通過本文能幫助到大家,讓大家理解掌握這部分內(nèi)容,需要的朋友可以參考下
    2017-10-10
  • Android日期選擇控件使用詳解

    Android日期選擇控件使用詳解

    這篇文章主要為大家詳細(xì)介紹了Android日期選擇控件的使用,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-08-08
  • Android編程調(diào)用Camera和相冊功能詳解

    Android編程調(diào)用Camera和相冊功能詳解

    這篇文章主要介紹了Android編程調(diào)用Camera和相冊功能,結(jié)合實例形式分析了Android的拍照及相冊調(diào)用功能相關(guān)實現(xiàn)技巧與操作注意事項,需要的朋友可以參考下
    2017-02-02
  • 詳解Flutter中StatefulBuilder組件的使用

    詳解Flutter中StatefulBuilder組件的使用

    StatefulBuilder小部件可以在這些區(qū)域的狀態(tài)發(fā)生變化時僅重建某些小區(qū)域而無需付出太多努力。本文將來詳細(xì)講講它的使用,需要的可以參考一下
    2022-05-05
  • Android 簡單服務(wù)定位器模式實現(xiàn)

    Android 簡單服務(wù)定位器模式實現(xiàn)

    這篇文章主要介紹了Android 簡單服務(wù)定位器模式實現(xiàn),幫助大家更好的理解和學(xué)習(xí)使用Android,感興趣的朋友可以了解下
    2021-03-03

最新評論

安乡县| 三台县| 韶山市| 宁阳县| 武汉市| 旬阳县| 静海县| 安达市| 阳新县| 民勤县| 贞丰县| 垣曲县| 沙坪坝区| 比如县| 甘孜| 同仁县| 崇左市| 建昌县| 隆尧县| 醴陵市| 茌平县| 罗平县| 博湖县| 长宁区| 安徽省| 安岳县| 惠水县| 澎湖县| 商丘市| 大渡口区| 水富县| 左云县| 同江市| 泽州县| 江阴市| 贵德县| 达州市| 铜鼓县| 将乐县| 苍山县| 工布江达县|