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
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res/com.muke.mukemytopbar"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<com.muke.mukemytopbar.Topbar
android:id="@+id/topbar"
android:layout_width="match_parent"
android:layout_height="40dp"
custom:leftBackground="@drawable/blue_button"
custom:leftText="Back"
custom:leftTextColor="#FFFFFF"
custom:rightBackground="@drawable/blue_button"
custom:rightTextColor="#FFFFFF"
custom:rightText="MORE"
custom:title="自定义标题"
custom:titleTextColor="#123412"
custom:titleTextSize="10sp"
></com.muke.mukemytopbar.Topbar>
</RelativeLayout>
atts.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="Topbar">
<!-- format指定义资源的类型 -->
<attr name="title" format="string"/>
<attr name="titleTextSize" format="dimension"/>
<attr name="titleTextColor" format="color"/>
<attr name="leftText" format="string"/>
<attr name="leftBackground" format="reference|color"/>
<attr name="leftTextColor" format="color"/>
<attr name="rightText" format="string"/>
<attr name="rightBackground" format="reference|color"/>
<attr name="rightTextColor" format="color"/>
</declare-styleable>
</resources>