• 手机版

    扫码体验手机版

  • 微信公众号

    扫码关注公众号

国内首家协议开发

软芯音视解码保护平台

在线
客服

发布
需求

在线
聊天

天盟
APP

天盟APP下载

关注
微信

微信扫一扫访问
顶部

自定义view?

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);
            //相当于初始化atts中的属性,在下面的方法中有两个默认的参数常用到,index,defvalue:默认值
                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();//回收避免浪费资源,清除缓存
                //初始化用到的三个控件
                leftButton=new Button(context);
                rightButton=new Button(context);
                tvTitle=new TextView(context);
                //把用到的控件和新定义的属性关联在一起
                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);
                //设置tvTitlt居中
                tvTitle.setGravity(Gravity.CENTER);
                //给viewgroup添加背景色
                setBackgroundColor(0xfff59563);
               
                //定义leftParam的狂傲属性
                leftParam=new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
                //给leftParam增加一个规则,在viewGroup中居左对齐
                leftParam.addRule(RelativeLayout.ALIGN_PARENT_LEFT,TRUE);
                //把leftButton加入到leftParam中
                addView(leftButton,leftParam);
               
                rightParam=new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
                //给leftParam增加一个规则,在viewGroup中居左对齐
                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

错误是什么?

使用道具 举报

发新帖

发布任务需求已有1031166位用户正在使用天盟网服务

发布分类: *
任务预算: *
需求内容: *
手机号码: *
任务商家报价为
  • 预算价 :
  • 成交价 :
  • 完工期 :
  • 质保期 :

* 最终任务项目以服务商报价、双方协商为准!