自定义view

view的自定义使用不仅仅包括在xml的页面进行赋值和布局,也可以在java中直接用语言进行布局,从而在xml中调用代码形成的模板。

MainActivity

package com.muke.mukemytopbar;

import com.muke.mukemytopbar.Topbar.tobarClickListener;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.Toast;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Topbar tobar=(Topbar) findViewById(R.id.topbar);
        
        tobar.setOntobarListener(new tobarClickListener() {
			
			@Override
			public void rightOnclic() {
				// TODO Auto-generated method stub
				
				System.out.println("QQQQQQQQQQQQ");
			Toast.makeText(MainActivity.this, "Right", Toast.LENGTH_LONG).show();	
			}
			
			@Override
			public void leftOnclic() {
				// TODO Auto-generated method stub
				System.out.println("ZZZZZZZZZZZZZ");
				Toast.makeText(MainActivity.this, "LEFT", Toast.LENGTH_LONG).show();
			}
		});
        
        
        
        
        
        
    }


  
}

Tobar.java
package com.muke.mukemytopbar;


import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.location.GpsStatus.Listener;
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;

public class Topbar extends RelativeLayout {

	private Button leftButton,rightButton;
	private TextView tvTitle;
	
	
	
	private int leftTextColor;
	private Drawable leftBackground;
	private String leftText;
	
	private int rightTextColor;
	private Drawable rightBackground;
	private String rightText;
	
	private float titleTextSize;
	private int titleTextColor;
	private String title;
	
	private LayoutParams leftParams, rightParams,titleParams;
	private tobarClickListener clickListener;
	
	public interface tobarClickListener
	{
		public void leftOnclic();
		public void rightOnclic();
	}
	public void setOntobarListener(tobarClickListener clickListener)
	{
		this.clickListener=clickListener;
	}
	
	public Topbar(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		TypedArray  ta=context.obtainStyledAttributes(attrs, R.styleable.Topbar); 
		
	     leftTextColor=ta.getColor(R.styleable.Topbar_leftTextColor, 0);
	     leftBackground=ta.getDrawable(R.styleable.Topbar_leftBackground); 
	     leftText=ta.getString(R.styleable.Topbar_leftText);
		
	     rightTextColor=ta.getColor(R.styleable.Topbar_rightTextColor, 0);
	     rightBackground=ta.getDrawable(R.styleable.Topbar_rightBackground); 
	     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.setText(leftText);
	     leftButton.setBackground(leftBackground);
	     leftButton.setTextColor(leftTextColor);
	     
	     rightButton.setText(rightText);
	     rightButton.setBackground(rightBackground);
	     rightButton.setTextColor(rightTextColor);
	     
	     
	     tvTitle.setText(title);
	     tvTitle.setTextSize(titleTextSize);
	     tvTitle.setTextColor(titleTextColor);
	     tvTitle.setGravity(Gravity.CENTER);
	     setBackgroundColor(0xFF5663);
	     
	     
	     
	     leftParams=new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
	     leftParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, TRUE);	     
	     
		 addView(leftButton,leftParams);
		 
		 rightParams=new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
		 rightParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, TRUE);	     
		     
         addView(rightButton,rightParams);
		 
         titleParams=new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,android.view.ViewGroup.LayoutParams.MATCH_PARENT);
		 titleParams.addRule(RelativeLayout.CENTER_IN_PARENT, TRUE);	     
		     
         addView(tvTitle,titleParams);
         
         
         leftButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
			
				 System.out.println("EEEEEEEEEE");
				clickListener.leftOnclic();
				
			}
		});
         rightButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				System.out.println("FFFFFFFF");
				clickListener.rightOnclic();
				
			}
		});
         
         
         
         
	}



	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}
activity_main.xml




   



atts.xml



    
        
        
        
        
        
        
        
        
        
        
      






你可能感兴趣的:(安卓)