xmlns:android是XML命名空间,告诉Android开发工具你准备使用Android命名空间里的一些通用属性
在所有Android XML文件中最外层的标记必须使用这个命名空间
用户也可以自定义XML属性
1. values/attrs.xml定义属性
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="relative">
<enum name="icon_left" value="0" />
<enum name="icon_right" value="1" />
<enum name="icon_above" value="2" />
<enum name="icon_below" value="3" />
</attr>
<declare-styleable name="IconTextView">
<attr name="relative"/>
<attr name="my_icon" format="reference"/>
<attr name="my_text" format="string"/>
<attr name="text_size" format="dimension"/>
<attr name="text_color" format="color"/>
<attr name="padding" format="dimension"/>
</declare-styleable>
</resources>
2. IconTextView.java继承LinearLayout实现自定义的属性
package com.silion.userdefinedxmlattr;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.media.Image;
import android.util.AttributeSet;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
/**
* Created by silion on 2015/10/19.
*/
public class IconTextView extends LinearLayout {
private final static int ICON_LEFT = 0;
private final static int ICON_RIGHT = 1;
private final static int ICON_ABOVE = 2;
private final static int ICON_BELOW = 3;
private int mRelative;
private Drawable mIcon;
private String mText;
private float mTextSize;
private Integer mTextColor;
private int mPadding;
private TextView mTextView;
private ImageView mImageView;
public IconTextView(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = null;
try {
a = context.obtainStyledAttributes(attrs, R.styleable.IconTextView);
mRelative = a.getInt(R.styleable.IconTextView_relative, 0);
mIcon = a.getDrawable(R.styleable.IconTextView_my_icon);
mText = a.getString(R.styleable.IconTextView_my_text);
mTextSize = a.getDimension(R.styleable.IconTextView_text_size, 15);
mTextColor = a.getInteger(R.styleable.IconTextView_text_color, 0);
mPadding = a.getInt(R.styleable.IconTextView_padding, 15);
} finally {
a.recycle();
}
mTextView = new TextView(context);
mTextView.setText(mText);
mTextView.setTextSize(mTextSize);
mTextView.setTextColor(mTextColor);
mImageView = new ImageView(context);
mImageView.setImageDrawable(mIcon);
int paddingLeft = 0;
int paddingRight = 0;
int paddingTop = 0;
int paddingBottom = 0;
int orientation = HORIZONTAL;
switch (mRelative) {
case ICON_LEFT: {
orientation = HORIZONTAL;
paddingLeft = mPadding;
break;
}
case ICON_RIGHT: {
orientation = HORIZONTAL;
paddingRight = mPadding;
break;
}
case ICON_ABOVE: {
orientation = VERTICAL;
paddingTop = mPadding;
break;
}
case ICON_BELOW: {
orientation = VERTICAL;
paddingBottom = mPadding;
break;
}
default:
break;
}
this.setOrientation(orientation);
mImageView.setPadding(paddingLeft, paddingTop, paddingRight, paddingBottom);
this.addView(mImageView);
this.addView(mTextView);
}
}
3. 在layout/activity_main.xml中定义xmlns使用自定义的属性
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<com.silion.userdefinedxmlattr.IconTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:relative="icon_right"
app:my_icon="@mipmap/ic_launcher"
app:my_text="@string/hello_world"
app:text_size="19sp"
app:text_color="#0000ff"
android:padding="16dp"/>
</LinearLayout>
4.MainActivity.java
package com.silion.userdefinedxmlattr;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}