自定义控件继承父类空间的时候,会继承父类控件的全部属性,但是有时候需要自己新增添属性满足开发的需要,这个时候需要在文件中进行配置,比如自定义控件com.ldw.market.view.RatioLayout,需要在这里里面增添一个新的属性宽高比ratio,首先需要在res/values/attrs.xml中新增添一个资源描述文件,这个文件中有针对的自定义控件的名字,属性的名字和参数类型,可以参照Android自己的属性进行定义。
res/values/attrs.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="com.ldw.market.view.RatioLayout">
<attr name="ratio" format="float"></attr>
</declare-styleable>
</resources>
在自定义控件的布局中需要新增命名空间namespace,并添加上新增的属性
新增的命名空间xmlns:ldw="http://schemas.android.com/apk/res/com.ldw.market"
布局文件中使用的时候需要带上命名空间ldw:ratio
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ldw="http://schemas.android.com/apk/res/com.ldw.market"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<!-- subject的单独的条目 -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="9dip"
android:layout_marginRight="9dip"
android:background="@drawable/list_item_bg" >
<!-- 使用一个自定义的FrameLayout -->
<com.ldw.market.view.RatioLayout
android:id="@+id/rl_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
ldw:ratio="2.73">
<ImageView
android:id="@+id/item_icon"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="fitCenter"
android:src="@drawable/ic_default" />
</com.ldw.market.view.RatioLayout>
<TextView
android:id="@+id/item_txt"
style="@style/TitleStyle"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_below="@id/rl_layout"
android:gravity="center_vertical"
android:paddingBottom="5dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:singleLine="true" />
</RelativeLayout>
</FrameLayout>
在逻辑代码中使用的方法是getAttributeFloatValue,并通过setRatio设置参数
package com.ldw.market.view;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.FrameLayout;
/*
* 按宽高比例显示图片
*/
public class RatioLayout extends FrameLayout{
//让这个容器按照一定的宽高比例显示
private float ratio = 2.43f; // 初始化比例值
public RatioLayout(Context context) {
super(context);
}
public RatioLayout(Context context, AttributeSet attrs) {
this(context, attrs, 0);//自动调用有三个参数的构造方法
}
public RatioLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
//参数1是命名空间,参数2是属性名字,参数三是默认的值
float ratio = attrs.getAttributeFloatValue("http://schemas.android.com/apk/res/com.ldw.market","ratio", 2.13f);
setRatio(ratio);//获取到参数,并设置
}
// 测量当前布局
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// widthMeasureSpec 宽度的规则 包含了两部分 模式 值
int widthMode = MeasureSpec.getMode(widthMeasureSpec); // 测量的模式
int widthSize = MeasureSpec.getSize(widthMeasureSpec);// 宽度大小
int width = widthSize - getPaddingLeft() - getPaddingRight();// 去掉左右两边的padding
int heightMode = MeasureSpec.getMode(heightMeasureSpec); // 模式
int heightSize = MeasureSpec.getSize(heightMeasureSpec);// 高度大小
int height = heightSize - getPaddingTop() - getPaddingBottom();// 去掉上下两边的padding
//如果宽度是一个精确值,也就是宽度是真实值match_parent,模式对应MeasureSpec.EXACTLY
if (widthMode == MeasureSpec.EXACTLY
&& heightMode != MeasureSpec.EXACTLY) {
// 修正一下 高度的值 让高度=宽度/比例
height = (int) (width / ratio + 0.5f); // 保证4舍五入
} else if (widthMode != MeasureSpec.EXACTLY//高度是一个精确值,修改宽度的
&& heightMode == MeasureSpec.EXACTLY) {
// 由于高度是精确的值 ,宽度随着高度的变化而变化
width = (int) ((height * ratio) + 0.5f);
}
// 重新制作了新的规则,把模式都修改
widthMeasureSpec = MeasureSpec.makeMeasureSpec(MeasureSpec.EXACTLY,
width + getPaddingLeft() + getPaddingRight());
heightMeasureSpec = MeasureSpec.makeMeasureSpec(MeasureSpec.EXACTLY,
height + getPaddingTop() + getPaddingBottom());
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}