首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > 移动开发 >

linearlayout承袭扩展篇

2012-09-22 
linearlayout继承扩展篇前面写了一个一个linearLayout,只不过那个只是继承了一个属性,在群聊的时候有人问

linearlayout继承扩展篇

前面写了一个一个linearLayout,只不过那个只是继承了一个属性,在群聊的时候有人问 想在扩展的时候添加几个按钮,作为一个封装用,于是我就试了一下效果还不错:我做了一个人工的进度条 可以加可以减

主函数很简单就一句话setContentView(R.layout.main);

相比大家都明白所有的东西 都在main.xml中

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res/co.android.widget"android:orientation="horizontal"android:layout_width="fill_parent"android:layout_height="wrap_content"android:paddingTop="5px"><TextViewandroid:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Meter:"/><com.commonsware.android.widget.Meterandroid:id="@+id/meter"android:layout_width="fill_parent"android:layout_height="wrap_content"app:max="100"app:incr="1"app:decr="5"/></LinearLayout>。红色部分是我自己加的属性啊 ,一定注意包名co.android.widget 

?

通过上面可以看到 没有prossbar,也没有加喝减的按钮啊,其实想想就知道了全在co.android.widget.Meter这里面。

那么就来看看Meter里面有啥:

public class Meter extends LinearLayout {private int max=100;private int incrAmount=1;private int decrAmount=-1;private ProgressBar bar=null;private View.OnClickListener onIncr=null;private View.OnClickListener onDecr=null;public Meter(final Context ctxt, AttributeSet attrs) {super(ctxt, attrs);this.setOrientation(HORIZONTAL);TypedArray a=ctxt.obtainStyledAttributes(attrs,R.styleable.Meter,0, 0);max=a.getInt(R.styleable.Meter_max, 100);incrAmount=a.getInt(R.styleable.Meter_incr, 1);decrAmount=-1*a.getInt(R.styleable.Meter_decr, 1);a.recycle();}/*public void setOnIncrListener(View.OnClickListener onIncr) {this.onIncr=onIncr;}public void setOnDecrListener(View.OnClickListener onDecr) {this.onDecr=onDecr;}public void setProgress(int progress) {bar.setProgress(progress);}public void setMax(int max) {this.max=max;bar.setMax(max);}*/@Overrideprotected void onFinishInflate() {super.onFinishInflate();((Activity)getContext()).getLayoutInflater().inflate(R.layout.meter, this);bar=(ProgressBar)findViewById(R.id.bar);bar.setMax(max);ImageButton btn=(ImageButton)findViewById(R.id.incr);btn.setOnClickListener(new View.OnClickListener() {public void onClick(View v) {bar.incrementProgressBy(incrAmount);if (onIncr!=null) {onIncr.onClick(Meter.this);}}});btn=(ImageButton)findViewById(R.id.decr);btn.setOnClickListener(new View.OnClickListener() {public void onClick(View v) {bar.incrementProgressBy(decrAmount);if (onDecr!=null) {onDecr.onClick(Meter.this);}}});}}

?我用不同颜色标注的部分 自己悟吧,就是怎么引用自定义属性。

你如果和上一篇比较就知道了 就多了一个onFinishInflate()?? 在这个方法中引用了另一个xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="horizontal"android:layout_width="fill_parent"android:layout_height="wrap_content"><ImageButton android:id="@+id/decr"android:layout_height="30px"android:layout_width="30px"android:src="@drawable/decr"/><ProgressBar android:id="@+id/bar"style="?android:attr/progressBarStyleHorizontal"android:layout_width="0px"android:layout_weight="1"android:layout_height="wrap_content"/><ImageButton android:id="@+id/incr"android:layout_height="30px"android:layout_width="30px"android:src="@drawable/incr"/></LinearLayout>

?

attr就很简单了

<resources><declare-styleable name="Meter"><attr name="max" format="integer" /><attr name="incr" format="integer" /><attr name="decr" format="integer" /></declare-styleable></resources>

?

热点排行