当前位置 : 主页 > 编程语言 > c++ >

自定义ToopBar /自定义控件

来源:互联网 收集:自由互联 发布时间:2021-07-03
attrs 自定义UI ToopBar.java 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.View;import andro
attrs
 

 

   
   
    
    
    
    
    
    
    
    
    
  

 
自定义UI ToopBar.java
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.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.johnsun.newui.R;

import sun.test.mode.TopBarCLickListener;

/**
 * Created by JohnSun on 2016/10/13.
 */
public class TopBar extends RelativeLayout {
    public TopBar(Context context) {
        super(context);
    }
    private int mLeftTextColor;
     private Drawable mLeftBackground;
     private String mLeftText;

    private int mRightTextColor;
    private Drawable mRightBackground;
    private String mRightText ;

    private String mTitle;

    private float titleTextSize;
    private int titleColor;

    private Button mLeftButton;
    private Button mRightButton;
    private TextView titleView;


    private TopBarCLickListener topBarCLickListener;


    private LayoutParams leftLayoutParams;
    private LayoutParams rightLayoutParams;
    private LayoutParams titleLayoutParams;

    public TopBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray typedArray=context.obtainStyledAttributes(attrs, R.styleable.TopBar);
        mLeftTextColor= typedArray.getColor(R.styleable.TopBar_leftTextColor,0);
        mLeftBackground=typedArray.getDrawable(R.styleable.TopBar_leftBackground);
        mRightTextColor=typedArray.getColor(R.styleable.TopBar_rightTextColor,0);
        mRightBackground=typedArray.getDrawable(R.styleable.TopBar_rightBackground);
        mRightText=typedArray.getString(R.styleable.TopBar_rightText);
        mTitle=typedArray.getString(R.styleable.TopBar_topTitle);
        titleTextSize=typedArray.getDimension(R.styleable.TopBar_topTitleTextSize,50);
        titleColor=typedArray.getColor(R.styleable.TopBar_topTitleTextColor,0);

        typedArray.recycle();

        initComponent(context);

        initClick();

    }

    public void initComponent(Context context){
        mLeftButton=new Button(context);
        mRightButton=new Button(context);
        titleView=new TextView(context);

        mLeftButton.setTextColor(mLeftTextColor);
        mLeftButton.setBackground(mLeftBackground);
        mLeftButton.setText(mLeftText);
        mRightButton.setTextColor(mRightTextColor);
        mRightButton.setBackground(mRightBackground);
        mRightButton.setText(mRightText);

        titleView.setText(mTitle);
        titleView.setTextColor(titleColor);
        titleView.setTextSize(titleTextSize);
        titleView.setGravity(Gravity.CENTER);

         // 设置布局
        leftLayoutParams=new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
        leftLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT,TRUE);
        addView(mLeftButton,leftLayoutParams);
        rightLayoutParams=new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
        rightLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,TRUE);
        addView(mRightButton,rightLayoutParams);

        titleLayoutParams=new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
        titleLayoutParams.addRule(RelativeLayout.CENTER_IN_PARENT,TRUE);
        addView(titleView,titleLayoutParams);

    }


    public void initClick(){
        mRightButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                topBarCLickListener.rightClick();
            }
        });

        mLeftButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                topBarCLickListener.leftClick();
            }
        });
    };


    public void setTopBarCLickListener(TopBarCLickListener topBarCLickListener) {

        this.topBarCLickListener = topBarCLickListener;
    }

    public void setButtonVisable( int id ,boolean flag){
        if (flag){
            if (id==0){
                mLeftButton.setVisibility(View.VISIBLE);
            }else {
                mRightButton.setVisibility(View.VISIBLE);
            }

        }else {
            if (id==0){
                mLeftButton.setVisibility(View.GONE);
            }
            else {
                mRightButton.setVisibility(View.GONE);
            }
        }


    }

}
资源
 

     
 
     
     
   public interface TopBarCLickListener {
    void leftClick();
    void rightClick();

}
使用自定义控件
private void initTopBar() {
        TopBar topBar= (TopBar) findViewById(R.id.topBar);
        topBar.setTopBarCLickListener(new TopBarCLickListener() {
            @Override
            public void leftClick() {
                Toast.makeText(MainActivity.this,"返回",Toast.LENGTH_SHORT).show();
            }
            @Override
            public void rightClick() {
//                JSONObject

                intNotification();
            }
        });
    }

   public void  intNotification(){
       NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
       PendingIntent pendingIntent3 = PendingIntent.getActivity(this, 0,
               new Intent(this, MainActivity.class), 0);
       // 通过Notification.Builder来创建通知,注意API Level
       // API16之后才支持
       Notification notify3 = new Notification.Builder(this)
               .setSmallIcon(R.drawable.ic_launcher)
               .setTicker("TickerText:" + "您有新短消息,请注意查收!")
               .setContentTitle("Notification Title")
               .setContentText("This is the notification message")
               .setContentIntent(pendingIntent3).setNumber(1).build(); // 需要注意build()是在API
       // level16及之后增加的,API11可以使用getNotificatin()来替代
       notify3.flags |= Notification.FLAG_AUTO_CANCEL; // FLAG_AUTO_CANCEL表明当通知被用户点击时,通知将被清除。
       manager.notify(NOTIFICATION, notify3);// 步骤4:通过通知管理器来发起通知。如果id不同,则每click,在status哪里增加一个提示

   }
网友评论