編輯:關於Android編程
1、組合View
2、繼承重寫View
3、全寫View
組合View,就是組合一些View來形成一個新的View。例如QQ的頭部欄
**
android:layout_width="match_parent"
上面這個前綴android是根據 根控件的
xmlns:**android**="http://schemas.android.com/apk/res/android"
這個來命名的,這是系統的。我們要自定義View的屬性,就加這個屬性
xmlns:app="http://schemas.android.com/apk/res-auto"
app就是我們定義的前綴名,當然你可以隨便修改。
實現類裡面通過下面的代碼來獲取到自定義的屬性,
TypedArray ta = mContext.obtainStyledAttributes(attrs,R.styleable.TopBar);
1、atts.xml , 自定義View 的屬性 ,在values下新建文件atts.xml
2、新建TopBar.java ,自定義view的內容類
public class TopBar extends LinearLayout {
//控件
private Button btHeadLeft;
private TextView tvHeadTitle;
private Button btHeadRight;
//左邊的Button
private String leftText;
private Drawable leftBack;
private int leftTextColor;
private float leftTextSize;
//右邊的Button
private String rightText;
private Drawable rightBack;
private int rightTextColor;
private float rightTextSize;
//中間的標題
private String titleText;
private Drawable titleBack;
private int titleTextColor;
private float titleTextSize;
private Context mContext;
private LayoutParams leftParams,rightParams,titleParams;
public TopBar(Context context) {
super(context);
}
public TopBar(Context context, AttributeSet attrs) {
super(context, attrs);
this.mContext = context;
initView(attrs);
initListener();
}
/**
* 初始化View
* @param attrs atts文件
*/
private void initView(AttributeSet attrs){
//獲取自定義View的值
TypedArray ta = mContext.obtainStyledAttributes(attrs,R.styleable.TopBar);
rightText = ta.getString(R.styleable.TopBar_rightText);
rightBack = ta.getDrawable(R.styleable.TopBar_rightBack);
rightTextColor = ta.getColor(R.styleable.TopBar_rightTextColor,0);
rightTextSize = ta.getDimension(R.styleable.TopBar_rightTextSize,0);
leftText = ta.getString(R.styleable.TopBar_leftText);
leftBack = ta.getDrawable(R.styleable.TopBar_leftBack);
leftTextColor = ta.getColor(R.styleable.TopBar_leftTextColor,0);
leftTextSize = ta.getDimension(R.styleable.TopBar_leftTextSize,0);
titleText = ta.getString(R.styleable.TopBar_title);
titleBack = ta.getDrawable(R.styleable.TopBar_titleBack);
titleTextColor = ta.getColor(R.styleable.TopBar_titleTextColor,0);
titleTextSize = ta.getDimension(R.styleable.TopBar_titleTextSize,0);
ta.recycle(); //回收
//初始化控件
btHeadLeft = new Button(mContext);
tvHeadTitle = new TextView(mContext);
btHeadRight = new Button(mContext);
//為控件設置值
btHeadLeft.setText(leftText);
btHeadLeft.setTextColor(leftTextColor);
btHeadLeft.setTextSize(leftTextSize);
btHeadLeft.setBackground(leftBack);
btHeadRight.setText(rightText);
btHeadRight.setTextColor(rightTextColor);
btHeadRight.setTextSize(rightTextSize);
btHeadRight.setBackground(rightBack);
tvHeadTitle.setText(titleText);
tvHeadTitle.setTextColor(titleTextColor);
tvHeadTitle.setTextSize(titleTextSize);
tvHeadTitle.setBackground(titleBack);
//設置View的背景顏色
this.setBackgroundColor(0xFF593363);
//設置布局屬性
leftParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
rightParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
titleParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
titleParams.weight = 1;
tvHeadTitle.setGravity(Gravity.CENTER);
this.addView(btHeadLeft,leftParams);
this.addView(tvHeadTitle,titleParams);
this.addView(btHeadRight,rightParams);
}
/**
* 初始化回調事件
*/
private void initListener(){
btHeadLeft.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
iTopBarOnClicklistener.leftOnClick();
}
});
btHeadRight.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
iTopBarOnClicklistener.rightOnClick();
}
});
tvHeadTitle.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
iTopBarOnClicklistener.titleOnClick();
}
});
}
public Button getBtHeadLeft() {
return btHeadLeft;
}
public void setBtHeadLeft(Button btHeadLeft) {
this.btHeadLeft = btHeadLeft;
}
public TextView getTvHeadTitle() {
return tvHeadTitle;
}
public void setTvHeadTitle(TextView tvHeadTitle) {
this.tvHeadTitle = tvHeadTitle;
}
public Button getBtHeadRight() {
return btHeadRight;
}
public void setBtHeadRight(Button btHeadRight) {
this.btHeadRight = btHeadRight;
}
public String getLeftText() {
return leftText;
}
public void setLeftText(String leftText) {
this.leftText = leftText;
}
public Drawable getLeftBack() {
return leftBack;
}
public void setLeftBack(Drawable leftBack) {
this.leftBack = leftBack;
}
public int getLeftTextColor() {
return leftTextColor;
}
public void setLeftTextColor(int leftTextColor) {
this.leftTextColor = leftTextColor;
}
public float getLeftTextSize() {
return leftTextSize;
}
public void setLeftTextSize(float leftTextSize) {
this.leftTextSize = leftTextSize;
}
public String getRightText() {
return rightText;
}
public void setRightText(String rightText) {
this.rightText = rightText;
}
public Drawable getRightBack() {
return rightBack;
}
public void setRightBack(Drawable rightBack) {
this.rightBack = rightBack;
}
public int getRightTextColor() {
return rightTextColor;
}
public void setRightTextColor(int rightTextColor) {
this.rightTextColor = rightTextColor;
}
public float getRightTextSize() {
return rightTextSize;
}
public void setRightTextSize(float rightTextSize) {
this.rightTextSize = rightTextSize;
}
public String getTitleText() {
return titleText;
}
public void setTitleText(String titleText) {
this.titleText = titleText;
}
public Drawable getTitleBack() {
return titleBack;
}
public void setTitleBack(Drawable titleBack) {
this.titleBack = titleBack;
}
public int getTitleTextColor() {
return titleTextColor;
}
public void setTitleTextColor(int titleTextColor) {
this.titleTextColor = titleTextColor;
}
public float getTitleTextSize() {
return titleTextSize;
}
public void setTitleTextSize(float titleTextSize) {
this.titleTextSize = titleTextSize;
}
public Context getmContext() {
return mContext;
}
public void setmContext(Context mContext) {
this.mContext = mContext;
}
public LayoutParams getLeftParams() {
return leftParams;
}
public void setLeftParams(LayoutParams leftParams) {
this.leftParams = leftParams;
}
public LayoutParams getRightParams() {
return rightParams;
}
public void setRightParams(LayoutParams rightParams) {
this.rightParams = rightParams;
}
public LayoutParams getTitleParams() {
return titleParams;
}
public void setTitleParams(LayoutParams titleParams) {
this.titleParams = titleParams;
}
/**
* 顯示隱藏左邊的View
* @param vis View.VISIABLE ...
*/
public void setLeftVisibility(int vis){
btHeadLeft.setVisibility(vis);
}
/**
* 顯示隱藏右邊的View
* @param vis View.VISIABLE ...
*/
public void setRightVisibility(int vis){
btHeadRight.setVisibility(vis);
}
//點擊的回調接口
public void setiTopBarOnClicklistener(ITopBarOnClicklistener iTopBarOnClicklistener) {
this.iTopBarOnClicklistener = iTopBarOnClicklistener;
}
private ITopBarOnClicklistener iTopBarOnClicklistener;
public interface ITopBarOnClicklistener{
void leftOnClick();
void titleOnClick();
void rightOnClick();
}
public void setTopBarOnClickListener(ITopBarOnClicklistener iTopBarOnClicklistener){
this.iTopBarOnClicklistener = iTopBarOnClicklistener;
}
}
MainActivty.java
public class MainActivity extends AppCompatActivity implements TopBar.ITopBarOnClicklistener{
private TopBar tbHeader;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tbHeader = (TopBar) findViewById(R.id.tb_header);
tbHeader.setiTopBarOnClicklistener(this);
tbHeader.setLeftVisibility(View.GONE);
}
@Override
public void leftOnClick() {
Toast.makeText(this,"left",Toast.LENGTH_SHORT).show();
}
@Override
public void titleOnClick() {
Toast.makeText(this,"title",Toast.LENGTH_SHORT).show();
}
@Override
public void rightOnClick() {
Toast.makeText(this,"right",Toast.LENGTH_SHORT).show();
}
}
activity_main.xml
之前寫過一個二維碼掃描demo,用的Zxing的框架,點擊下載,後續掃描二維碼中出現一些問題,比如解決壓縮圖片,調整掃描窗口大小等等。後續單位要求做掃描登錄實現,發現難點
以文本和思維導圖的方式簡明扼要的介紹了GoF的23個經典設計模式,可當成學習設計模式的一個小手冊,偶爾看一下,說不定會對大師的思想精髓有新的領悟。GoF(“四
Android布局文件中的layout_weight屬性僅在LinearLayout布局中有效。google推薦:當設置了控件的layout_weight屬性時,該控件相
Android View分區域點擊實現方案——1.根據坐標范圍 標簽: Android學習記錄 引言 好哒,很久沒更新博客裡關於Android的博文了,