验证中...
私信发送成功
片段 1 片段 2 片段 3 片段 4
attrs
原始数据 复制代码
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="TopBar">
<attr name="topTitle" format="string"/>
<attr name="topTitleTextSize" format="dimension"/>
<attr name="topTitleTextColor" format="color"/>
<attr name="leftTextColor" format="color"/>
<attr name="leftBackground" format="reference|color"/>
<attr name="leftText" format ="string"/>
<attr name="rightTextColor" format="color"/>
<attr name="rightBackground" format="reference|color"/>
<attr name="rightText" format="string"/>
</declare-styleable>
</resources>
自定义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);
}
}
}
}
资源
原始数据 复制代码
<sun.test.TopBar
android:id="@+id/topBar"
android:layout_width="match_parent"
android:layout_height="80dp"
custom:leftBackground="@drawable/ic_google_messenger_icon"
custom:leftText="Back"
custom:leftTextColor="#FFFFFF"
custom:rightBackground="@drawable/ic_google_plus_icon"
custom:rightText="More"
custom:rightTextColor="#FFFFFF"
custom:topTitle="我是标题"
custom:topTitleTextColor="#f2fa09"
custom:topTitleTextSize="36sp"
>
</sun.test.TopBar>
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哪里增加一个提示
}

评论列表( 0 )

你可以在登录后,对此项目发表评论