通过onMeasure方法修改布局高跟宽的比例
2017-09-20 17:18
267 查看
试用于轮播图的屏幕宽跟高的固定比例
自己定义布局继承重写onMeasure方法
<com.sanwn.ddmb.view.AppBarLayoutView
android:id="@+id/app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:ratio="2">
<declare-styleable name="RatioLayout">
<attr name="ratio" format="float" />
</declare-styleable>
重写类是实例
public class AppBarLayoutView extends AppBarLayout {
private static final String TAG = "RatioLayout";
// 宽和高的比例
private float ratio = 0.0f;
public AppBarLayoutView(Context context) {
super(context);
}
public AppBarLayoutView(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.RatioLayout);
ratio = a.getFloat(R.styleable.RatioLayout_ratio, 0.0f);
a.recycle();
}
public void setRatio(float f) {
ratio = f;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int width = MeasureSpec.getSize(widthMeasureSpec) - getPaddingLeft() - getPaddingRight();
int height = MeasureSpec.getSize(heightMeasureSpec) - getPaddingTop() - getPaddingBottom();
if (widthMode == MeasureSpec.EXACTLY && heightMode != MeasureSpec.EXACTLY && ratio != 0.0f) {
height = (int) (width / ratio + 0.5f);
heightMeasureSpec = MeasureSpec.makeMeasureSpec(height + getPaddingTop() + getPaddingBottom(),
MeasureSpec.EXACTLY);
} else if (widthMode != MeasureSpec.EXACTLY && heightMode == MeasureSpec.EXACTLY && ratio != 0.0f) {
width = (int) (height * ratio + 0.5f);
widthMeasureSpec = MeasureSpec.makeMeasureSpec(width + getPaddingLeft() + getPaddingRight(),
MeasureSpec.EXACTLY);
}
Log.d(TAG, "onMeasure: ==="+height+"==="+width);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
自己定义布局继承重写onMeasure方法
<com.sanwn.ddmb.view.AppBarLayoutView
android:id="@+id/app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:ratio="2">
<declare-styleable name="RatioLayout">
<attr name="ratio" format="float" />
</declare-styleable>
重写类是实例
public class AppBarLayoutView extends AppBarLayout {
private static final String TAG = "RatioLayout";
// 宽和高的比例
private float ratio = 0.0f;
public AppBarLayoutView(Context context) {
super(context);
}
public AppBarLayoutView(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.RatioLayout);
ratio = a.getFloat(R.styleable.RatioLayout_ratio, 0.0f);
a.recycle();
}
public void setRatio(float f) {
ratio = f;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int width = MeasureSpec.getSize(widthMeasureSpec) - getPaddingLeft() - getPaddingRight();
int height = MeasureSpec.getSize(heightMeasureSpec) - getPaddingTop() - getPaddingBottom();
if (widthMode == MeasureSpec.EXACTLY && heightMode != MeasureSpec.EXACTLY && ratio != 0.0f) {
height = (int) (width / ratio + 0.5f);
heightMeasureSpec = MeasureSpec.makeMeasureSpec(height + getPaddingTop() + getPaddingBottom(),
MeasureSpec.EXACTLY);
} else if (widthMode != MeasureSpec.EXACTLY && heightMode == MeasureSpec.EXACTLY && ratio != 0.0f) {
width = (int) (height * ratio + 0.5f);
widthMeasureSpec = MeasureSpec.makeMeasureSpec(width + getPaddingLeft() + getPaddingRight(),
MeasureSpec.EXACTLY);
}
Log.d(TAG, "onMeasure: ==="+height+"==="+width);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
相关文章推荐
- 通过字符串调用方法得到结果及动态创建Swing组件和布局
- 通过修改ASP代码来实现Jmail发送邮件方法详解
- 通过jQuery的attr修改onclick值的的解决方法
- 浏览器 通过注册表来设置修改默认浏览器的方法
- 对于当浏览器通过ajax加载的文件还是修改前的文件的处理方法
- 通过PHP修改Linux或Unix口令的方法分享
- 简单完整的代码,通过这个代码你将对RSA加密算法在Java中的实现方法有一个初步的了解,这个类,你可以直接使用,水平高的,就自己修改完善下代码。
- Xcode 真机测试破解方法(转加修改)xcode 4.3 通过
- jQuery的change方法什么时候触发? 你绝对意想不到,即使你通过jQuery修改了input标签的value值(即修改内存中的DOM树中的input),也不会触发change事件,不会,不会
- CodeIgniter通过修改.htaccess文件的办法解决加载css js 图片类型文件的方法
- mysql通过my.cnf修改默认字符集为utf-8的方法和注意事项
- xcode 4 最低的要求是 10.6.6的版本,如果你是 10.6.3的版本,又不想升级的话。可以考虑通过修改版本号的方法进行安装
- Ubuntu修改默认键盘布局的方法(适用于13.04|14.04)
- openstack 管理三十九 - 通过修改数据库方法实现 VM 迁移
- 通过ListActivity使用ListView布局方法
- linux通过单用户修改用户密码的方法
- Xcode 真机测试破解方法(转加修改)xcode 4.3 通过
- IIS 之 通过 Web.config 修改文件上传大小限制设置方法
- 通过查询资料理解onMeasure()方法
- 用python脚本通过odoorpc方法修改odoo中字段内容