android自定义可任意调节比例的控件

在做android开发的时候,有时候我们需要用调解控件的比例,而不是固定控件的长度和宽度。在Java代码中调节的时候,有时候很麻烦,今天给大家介绍一种自定义固定比例的控件的方法,废话多说,直接上代码:

package com.android.widget;
import com.sun.R;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.widget.LinearLayout;
/**
 * 可调节长宽比例的LinearLayout,会自动根据宽度按比例调节高度
 * @author sunzheng
 * @date 2013.10.15
 * @tips xmlns:etion="http://schemas.android.com/apk/res/com.xuanwu.etion"<br>
 * 		etion:aspectRatio="float"<br>
 * 		etion:aspectRatioBase="integer":<br>
 * 		0=ASPECT_RATION_BASE_X, 1=ASPECT_RATION_BASE_Y<br>
 * */
public class ScaleLinearLayout extends LinearLayout {
	/**以X为基准进行比例缩放*/
	public static final int ASPECT_RATION_BASE_X=0;
	/**以Y为基准进行比例缩放*/
	public static final int ASPECT_RATION_BASE_Y=1;
	
	private float aspectRatio=0;
	private int aspectRatioBase=0;
	public ScaleLinearLayout(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		readAttrs(context, attrs);
	}

	public ScaleLinearLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
		readAttrs(context, attrs);
	}

	public ScaleLinearLayout(Context context) {
		super(context);
	}
	private void readAttrs(Context context, AttributeSet attrs){
		  TypedArray a = context.obta
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值