第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

自定義view?

自定義view?

負(fù)手_睥睨天下 2016-08-11 17:46:20
package com.sb.testzdyview;import android.annotation.SuppressLint;import android.content.Context;import android.content.res.TypedArray;import android.graphics.drawable.Drawable;import android.util.AttributeSet;import android.view.Gravity;import android.view.ViewGroup;import android.widget.Button;import android.widget.RelativeLayout;import android.widget.TextView;? @SuppressLint("NewApi") public class Topbar extends RelativeLayout { private Button leftButton,rightButton; private TextView tvTitle; private LayoutParams leftParam,rightParam,titleParam; private int leftTextColor; private Drawable leftBackgroud; private String leftText; private int rightTextColor; private Drawable rightBackgroud; private String rightText; private float titleTextSize; private int titleTextColor; private String title;? ? ?@SuppressLint("NewApi") public Topbar(Context context, AttributeSet attrs) { super(context, attrs); // 得到atts中的自定義屬相并賦給ta集合 TypedArray ta=context.obtainStyledAttributes(attrs, R.styleable.Topbar); ? ?//相當(dāng)于初始化atts中的屬性,在下面的方法中有兩個(gè)默認(rèn)的參數(shù)常用到,index,defvalue:默認(rèn)值 leftTextColor=ta.getColor(R.styleable.Topbar_leftTextColor, 0); leftBackgroud=ta.getDrawable(R.styleable.Topbar_leftBackgroud); leftText=ta.getString(R.styleable.Topbar_leftText); rightTextColor=ta.getColor(R.styleable.Topbar_leftTextColor, 0); rightBackgroud=ta.getDrawable(R.styleable.Topbar_leftBackgroud); rightText=ta.getString(R.styleable.Topbar_rightText); titleTextSize=ta.getDimension(R.styleable.Topbar_titleTextSize, 0); titleTextColor=ta.getColor(R.styleable.Topbar_titleTextColor, 0); title=ta.getString(R.styleable.Topbar_title); ta.recycle();//回收避免浪費(fèi)資源,清除緩存 //初始化用到的三個(gè)控件 leftButton=new Button(context); rightButton=new Button(context); tvTitle=new TextView(context); //把用到的控件和新定義的屬性關(guān)聯(lián)在一起 leftButton.setTextColor(leftTextColor); leftButton.setBackground(leftBackgroud); leftButton.setText(leftText); rightButton.setTextColor(rightTextColor); rightButton.setBackground(rightBackgroud); rightButton.setText(rightText); tvTitle.setTextSize(titleTextSize); tvTitle.setTextColor(titleTextColor); tvTitle.setText(title); //設(shè)置tvTitlt居中 tvTitle.setGravity(Gravity.CENTER); //給viewgroup添加背景色 setBackgroundColor(0xfff59563); //定義leftParam的狂傲屬性 leftParam=new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT); //給leftParam增加一個(gè)規(guī)則,在viewGroup中居左對(duì)齊 leftParam.addRule(RelativeLayout.ALIGN_PARENT_LEFT,TRUE); //把leftButton加入到leftParam中 addView(leftButton,leftParam); rightParam=new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT); //給leftParam增加一個(gè)規(guī)則,在viewGroup中居左對(duì)齊 rightParam.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,TRUE); //把leftButton加入到leftParam中 addView(rightButton,rightParam); titleParam=new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT); titleParam.addRule(RelativeLayout.CENTER_IN_PARENT,TRUE); addView(tvTitle,titleParam); }}
查看完整描述

1 回答

?
ziom

TA貢獻(xiàn)948條經(jīng)驗(yàn) 獲得超1109個(gè)贊

錯(cuò)誤是什么?

查看完整回答
反對(duì) 回復(fù) 2016-08-11
  • 1 回答
  • 0 關(guān)注
  • 1189 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)