亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

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

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

效果圖:

這里寫圖片描述

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

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屬性,對(duì)wrap_content設(shè)置一個(gè)指定值

@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);//獲取高度的測(cè)量模式

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

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

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

public interface onClick {
    void onLeftButtonClick();

    void onCenterButtonClick();

    void onRightButtonClick();
  }

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

private onClick onClick;

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

在添加按鈕的時(shí)候添加點(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();

      }
    });

最終效果:

這里寫圖片描述

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

相關(guān)文章

最新評(píng)論