android自定義組件實(shí)現(xiàn)方法
本文實(shí)例講述了android自定義組件實(shí)現(xiàn)方法。分享給大家供大家參考。具體如下:
atts.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="TopBar">
<attr name="titleText" 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>
TopBar.java:
package com.cd.administrator.mytopbar;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.os.Build;
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;
/**
* Created by Administrator on 2015/1/8.
*/
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 int titleTextColor;
private String titleText;
private float titleTextSize;
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;
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public TopBar(final 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);
titleTextColor = ta.getColor(R.styleable.TopBar_titleTextColor,0);
titleTextSize = ta.getDimension(R.styleable.TopBar_titleTextSize,0);
titleText = ta.getString(R.styleable.TopBar_titleText);
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(titleTextSize);
tvTitle.setText(titleText);
tvTitle.setGravity(Gravity.CENTER);
setBackgroundColor(0xf59563);
leftParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
leftParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT,TRUE);
addView(leftButton,leftParams);
rightParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
rightParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,TRUE);
addView(rightButton,rightParams);
titleParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
titleParams.addRule(RelativeLayout.CENTER_IN_PARENT,TRUE);
addView(tvTitle,titleParams);
leftButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
listener.leftClick();
}
});
rightButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
listener.rightClick();
}
});
}
}
activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<com.cd.administrator.mytopbar.TopBar
android:id="@+id/topBar"
android:layout_width="match_parent"
android:layout_height="40dp"
custom:leftBackground="@drawable/blue"
custom:leftText="Back"
custom:leftTextColor="#ffffff"
custom:rightBackground="@drawable/blue"
custom:rightText="More"
custom:rightTextColor="#ffffff"
custom:titleTextColor="#121212"
custom:titleTextSize="15sp"
custom:titleText="自定義標(biāo)題">
</com.cd.administrator.mytopbar.TopBar>
</RelativeLayout>
MainActivity.java:
package com.cd.administrator.mytopbar;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
@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 leftClick() {
Toast.makeText(MainActivity.this, "cd--left", Toast.LENGTH_SHORT).show();
}
@Override
public void rightClick() {
Toast.makeText(MainActivity.this,"cd--right",Toast.LENGTH_SHORT).show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
希望本文所述對(duì)大家的Android程序設(shè)計(jì)有所幫助。
- Android自定義組件獲取本地圖片和相機(jī)拍照?qǐng)D片
- Android 自定義組件成JAR包的實(shí)現(xiàn)方法
- Android自定義組件ListPopWindow
- Android實(shí)現(xiàn)Ant Design 自定義表單組件
- Android自定義加載loading view動(dòng)畫組件
- Android UI設(shè)計(jì)系列之自定義DrawView組件實(shí)現(xiàn)數(shù)字簽名效果(5)
- Android中自定義Checkbox組件實(shí)例
- Android自定義View設(shè)定到FrameLayout布局中實(shí)現(xiàn)多組件顯示的方法 分享
- Android編程自定義組件實(shí)例詳解
相關(guān)文章
Android中RecycleView與ViewPager沖突的解決方法及原理
這篇文章主要給大家介紹了關(guān)于Android中RecycleView與ViewPager沖突的解決方法及原理的相關(guān)資料,以及ViewPager嵌套R(shí)ecycleView卡頓問(wèn)題的處理方法,文中通過(guò)示例代碼介紹的非常狎昵,需要的朋友可以參考下2018-07-07
Android動(dòng)態(tài)時(shí)鐘壁紙開(kāi)發(fā)
這篇文章主要為大家詳細(xì)介紹了Android動(dòng)態(tài)時(shí)鐘壁紙開(kāi)發(fā)的相關(guān)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-01-01
ListView的Adapter使用(綁定數(shù)據(jù)) 之 自定義每一項(xiàng)的布局去綁定數(shù)據(jù)
之前寫的綁定數(shù)據(jù)是只是簡(jiǎn)單的綁定了字符串,這次我們將一次綁定多條數(shù)據(jù)并且嘗試用自定義的布局。在這篇文章中首先講解的是用Hashmap 去綁定數(shù)據(jù),第二個(gè)例子,講解自定義布局然后綁定數(shù)據(jù)2013-06-06
flutter BottomAppBar實(shí)現(xiàn)不規(guī)則底部導(dǎo)航欄
這篇文章主要為大家詳細(xì)介紹了flutter BottomAppBar實(shí)現(xiàn)不規(guī)則底部導(dǎo)航欄,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-07-07
android7.0實(shí)現(xiàn)分享圖片到朋友圈功能
這篇文章主要為大家詳細(xì)介紹了android7.0實(shí)現(xiàn)分享圖片到朋友圈功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05
Android自定義View中attrs.xml的實(shí)例詳解
這篇文章主要介紹了Android自定義View中attrs.xml的實(shí)例詳解的相關(guān)資料,在自定義View首先對(duì)attrs.xml進(jìn)行布局的實(shí)現(xiàn)及屬性的應(yīng)用,需要的朋友可以參考下2017-07-07
封裝的android監(jiān)聽(tīng)手指左右滑動(dòng)屏幕的事件類分享
這篇文章主要介紹了封裝的android監(jiān)聽(tīng)手指左右滑動(dòng)屏幕的事件類分享,本文分別給出了簡(jiǎn)單處理方法的代碼和封裝好的處理類代碼,需要的朋友可以參考下2015-05-05
Android Studio 中運(yùn)行 groovy 程序的方法圖文詳解
這篇文章主要介紹了Android Studio 中 運(yùn)行 groovy 程序的方法,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-03-03

