topbar顏色不改變
setbackground出錯 提示讓改成setbackgrounddrawable
topbar.java
package com.example.mytopbar;
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 android.widget.Toast;
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 tltleTextSize;
private int titleTextColor;
private String title;
private LayoutParams leftParams, rightParams, titleParams;
private topbarClickListener listener;
public interface topbarClickListener {
public void leftClick();
public void rightClick();
}
public void setOnTopbarClickListener(topbarClickListener listener) {
this.listener = listener;
}
public Topbar(Context context, AttributeSet attrs) {
super(context, attrs);
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);
tltleTextSize = 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(leftBackground);
leftButton.setText(leftText);
rightButton.setTextColor(rightTextColor);
rightButton.setBackground(rightBackground);
rightButton.setText(rightText);
tvTitle.setTextColor(titleTextColor);
tvTitle.setTextSize(tltleTextSize);
tvTitle.setText(title);
tvTitle.setGravity(Gravity.CENTER);
setBackgroundColor(0xff59563);
leftParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
rightParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, TRUE);
addView(leftButton, leftParams);
rightParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
rightParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, TRUE);
addView(rightButton, rightParams);
titleParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
LayoutParams.MATCH_PARENT);
titleParams.addRule(RelativeLayout.CENTER_IN_PARENT, TRUE);
addView(tvTitle, titleParams);
leftButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
listener.leftClick();
}
});
rightButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
listener.rightClick();
}
});
}
public void setLeftIsvisibable(boolean flag)
{
if(flag){
leftButton.setVisibility(View.VISIBLE);
}else{
leftButton.setVisibility(View.GONE);
}
}
}
mainactivity.java
package com.example.mytopbar;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Topbar topbar=(Topbar)findViewById(R.id.topbar);
topbar.setOnTopbarClickListener(new Topbar.topbarClickListener() {
@Override
public void rightClick() {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "IMOOC_LEFT", Toast.LENGTH_SHORT).show();
}
@Override
public void leftClick() {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "IMOOC_RIGHT", Toast.LENGTH_SHORT).show();
}
});
}
//topbar.setLeftIsVisable(false);
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
? ? xmlns:custom="http://schemas.android.com/apk/res/com.example.mytopbar"
? ? xmlns:tools="http://schemas.android.com/tools"
? ? android:layout_width="match_parent"
? ? android:layout_height="match_parent"
? ? android:padding="5dp"
? ? tools:context="com.example.mytopbar.MainActivity" >
? ? <com.example.mytopbar.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:rightText="More"
? ? ? ? custom:rightTextColor="#FFFFFF"
? ? ? ? custom:title="自定義標題"
? ? ? ? custom:titleTextColor="#123412"
? ? ? ? custom:titleTextSize="10sp" >
? ? </com.example.mytopbar.Topbar>
</RelativeLayout>
atts.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
? ? <declare-styleable name="Topbar">
? ? ? ? <attr name="title" format="string" />
? ? ? ? <attr name="titleTextSize" format="dimension"></attr>
? ? ? ? <attr name="titleTextColor" format="color"></attr>
? ? ? ? <attr name="leftTextColor" format="color"></attr>
? ? ? ? <attr name="leftBackground" format="reference|color"></attr>
? ? ? ? <attr name="leftText" format="string"></attr>
? ? ? ? <attr name="rightTextColor" format="color"></attr>
? ? ? ? <attr name="rightBackground" format="reference|color"></attr>
? ? ? ? <attr name="rightText" format="string"></attr>
? ? </declare-styleable>
</resources>
2016-07-24
Button 不是自己自定義的組件,所以要按系統的方法來設置屬性.一般IDE提示怎么做,就按提示的來