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)畫(huà)組件
- 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仿微信進(jìn)度彈出框的實(shí)現(xiàn)方法
最近公司項(xiàng)目需要實(shí)現(xiàn)類似微信進(jìn)度條彈出框效果,其實(shí)現(xiàn)方法并不難,下面給大家介紹下Android仿微信進(jìn)度彈出框的實(shí)現(xiàn)方法,需要的朋友參考下吧2017-01-01SQLSERVER實(shí)現(xiàn)更改表名,更改列名,更改約束代碼
這篇文章主要介紹了SQLSERVER實(shí)現(xiàn)更改表名,更改列名,更改約束代碼的相關(guān)資料,需要的朋友可以參考下2016-03-03Android DataBinding單向數(shù)據(jù)綁定深入探究
看了谷歌官方文章確實(shí)寫(xiě)的太簡(jiǎn)略了,甚至看完之后有很多地方還不知道怎么回事兒或者怎么用,那么接下來(lái)我將通過(guò)文章全面介紹一下DataBinding單向數(shù)據(jù)綁定2022-11-11Android實(shí)戰(zhàn)項(xiàng)目之實(shí)現(xiàn)一個(gè)簡(jiǎn)單計(jì)算器
隨著移動(dòng)互聯(lián)網(wǎng)的普及,手機(jī)應(yīng)用程序已經(jīng)成為人們生活中不可或缺的一部分,計(jì)算器是一類被廣泛使用的應(yīng)用程序之一,這篇文章主要給大家介紹了關(guān)于Android實(shí)戰(zhàn)項(xiàng)目之實(shí)現(xiàn)一個(gè)簡(jiǎn)單計(jì)算器的相關(guān)資料,需要的朋友可以參考下2023-10-10刷新Activity中的scrollview示例(局部ui刷新)
代碼很簡(jiǎn)單,但是很實(shí)用,適合在一個(gè)Activity中要刷新局部的UI,比如在掃描一維碼的時(shí)候,要把每次掃描的結(jié)果都顯示在界面上2014-01-01Android實(shí)現(xiàn)底部圖標(biāo)與Fragment的聯(lián)動(dòng)實(shí)例
本篇文章主要介紹了Android實(shí)現(xiàn)底部圖標(biāo)與Fragment的聯(lián)動(dòng)實(shí)例,具有一定的參考價(jià)值,有興趣的可以了解一下2017-07-07Android自定義View實(shí)現(xiàn)心形圖案
這篇文章主要為大家詳細(xì)介紹了Android自定義View實(shí)現(xiàn)心形圖案,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09