android自定义组件实现方法
程序员文章站
2022-07-11 18:22:18
本文实例讲述了android自定义组件实现方法。分享给大家供大家参考。具体如下:
atts.xml:
本文实例讲述了android自定义组件实现方法。分享给大家供大家参考。具体如下:
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="自定义标题"> </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); } }
希望本文所述对大家的android程序设计有所帮助。
上一篇: 汤菜怎么做汤,这样做既美味营养也不流失!
下一篇: 木瓜炖雪蛤的功效与作用