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"
>
	<TextView	
		android:layout_width="wrap_content" 
		android:layout_height="wrap_content" 
		android:text="Meter:"
	/>
	<com.commonsware.android.widget.Meter
		android: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);
	}
	*/
	@Override
	protected 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>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值