带自定义属性的组合控件在AS和eclipse中的写法发生了一些变化,先上代码再解释
主要体现在两个方面:自定义属性的获取和命名空间的声明。具体看代码
AS中的写法
1、布局文件,view_mine_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:background="@drawable/mine_item_click_selector"
android:layout_height="70dp">
<ImageView
android:id="@+id/iv_image"
android:src="@drawable/ssdk_oks_classic_evernote"
android:layout_margin="15dp"
android:layout_centerVertical="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/tv_title"
android:layout_toRightOf="@id/iv_image"
android:layout_centerVertical="true"
android:textSize="18sp"
android:text="北京市"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:src="@drawable/enter_pressed"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="15dp"
android:layout_width="15dp"
android:layout_height="30dp" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_alignParentBottom="true"
android:background="@color/gry_c"/>
</RelativeLayout>
2.自定义属性,在values中创建attrs.xml文件
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="MineItemClickView"> <-- 与你的自定义控件的名称相同-->
<attr name="titleText" format="string"/>
<attr name="titleImage" format="reference"/>
</declare-styleable>
</resources>
3. 创建自定义控件 MineItemClickView.java
public class MineItemClickView extends RelativeLayout {
private ImageView mIamge;
private TextView mTitle;
public MineItemClickView(Context context, AttributeSet attrs) {
super(context, attrs);
//加载attrs.xml文件中的自定义属性
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.MineItemClickView);
CharSequence title = ta.getText(R.styleable.MineItemClickView_titleText);
int iconId = ta.getResourceId(R.styleable.MineItemClickView_titleImage, 0);
View view = LayoutInflater.from(context).inflate(R.layout.view_mine_item,this,true);
mIamge = (ImageView) view.findViewById(R.id.iv_image);
mTitle = (TextView) view.findViewById(R.id.tv_title);
//赋值
mTitle.setText(title);
mIamge.setImageResource(iconId);
ta.recycle();
}
/**
* 设置标题
* @param text
*/
public void setTitleText(String text){
mTitle.setText(text);
}
/**
* 设置图标
* @param resId
*/
public void setTitleImage(int resId){
mIamge.setImageResource(resId);
}
<span style="font-size:12px;">}</span>
4.使用(注意命名空间是yun:android="http://schemas.android.com/apk/res-auto")
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:yun="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.zsc.blacksmith.fragment.OneFragment">
<pre name="code" class="html"> <com.zsc.blacksmith.view.widget.MineItemClickView
android:id="@+id/micv_beijing"
android:layout_width="match_parent"
android:layout_height="wrap_content"
yun:titleImage="@drawable/ssdk_oks_classic_evernote"
yun:titleText="北京市" />
</LinearLayout>
Eclipse中的写法
1.同AS中第一步
2.自定义属性
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="titleText" format="string"/>
<attr name="titleImage" format="reference"/>
</resources>
3. 创建自定义控件 MineItemClickView.java
public class MineItemtClickView extends RelativeLayout{
//命名空间
private static final String NAMESPACE = "http://schemas.android.com/apk/res/com.example.mymy";
private ImageView mIamge;
private TextView mTitle;
public MineItemtClickView(Context context) {
super(context);
}
public MineItemtClickView(Context context, AttributeSet attrs) {
super(context, attrs);
initView(context);
String title = attrs.getAttributeValue(NAMESPACE, "titleText");
int icon = attrs.getAttributeResourceValue(NAMESPACE, "titleImage",0);
setTitle(title);
mIamge.setImageResource(icon);
}
private void setTitle(String title) {
mTitle.setText(title);
}
private void initView(Context context){
View.inflate(context, R.layout.view_mine_item, this);
mIamge = (ImageView) findViewById(R.id.iv_image);
mTitle = (TextView) findViewById(R.id.tv_title);
}
}
4.使用(注意命名空间是yun:android="http://schemas.android.com/apk/res/android")
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yun="http://schemas.android.com/apk/res/包名"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.zsc.blacksmith.view.widget.MineItemClickView
android:layout_width="match_parent"
android:layout_height="wrap_content"
<pre name="code" class="html"><pre name="code" class="html"> yun:titleImage="@drawable/ssdk_oks_classic_evernote"
yun:titleText="北京市"/>
</RelativeLayout>