android 自定义LinearLayout

接着上一篇,本篇玩一下自定义LinearLayout,直接上代码:

1. attr.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <!-- MyTextView -->
    <declare-styleable name="MyTextView">
        <attr name="textColor" format="color" />
        <attr name="textSize" format="dimension" />
        <attr name="title" format="string" />
    </declare-styleable>

    <!-- MyLinearLayout -->
    <declare-styleable name="MyLinearLayout">
        <attr name="Text" format="reference|string"></attr>
        <attr name="Orientation">
            <enum name="Horizontal" value="1"></enum>
            <enum name="Vertical" value="0"></enum>
        </attr>
    </declare-styleable>

</resources>


2.继承LinearLayout重新定义一个:

package com.test.customviewtest;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MyLinearLayout extends LinearLayout {

	public MyLinearLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
		int resouceId = -1;
		TypedArray MyTypeArray = context.obtainStyledAttributes(attrs,
				R.styleable.MyLinearLayout);

		TextView tv = new TextView(context);
		EditText et = new EditText(context);

		int N = MyTypeArray.getIndexCount();// N=2
		for (int i = 0; i < N; i++) {
			int attr = MyTypeArray.getIndex(i);
			switch (attr) {

			case R.styleable.MyLinearLayout_Text:
				resouceId = MyTypeArray.getResourceId(
						R.styleable.MyLinearLayout_Text, 0);
				tv.setText(resouceId > 0 ? MyTypeArray.getResources().getText(
						resouceId) : MyTypeArray
						.getString(R.styleable.MyLinearLayout_Text));
				break;

			case R.styleable.MyLinearLayout_Orientation:
				resouceId = MyTypeArray.getInt(
						R.styleable.MyLinearLayout_Orientation, 0);
				this.setOrientation(resouceId == 1 ? LinearLayout.HORIZONTAL
						: LinearLayout.VERTICAL);
				break;
			}
		}
		addView(tv);
		addView(et);
		MyTypeArray.recycle();
	}
}


3.在activity_main.xml 布局中引用自定义LinearLayout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:mycustomapp="http://schemas.android.com/apk/res/com.test.customviewtest"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <!-- 属性:textColor、textSize、title   是attr.xml中定义的 -->


    <!-- <com.test.customviewtest.MyTextView -->
    <!-- android:layout_width="wrap_content" -->
    <!-- android:layout_height="wrap_content" -->
    <!-- mycustomapp:textColor="#00ff00" -->
    <!-- mycustomapp:textSize="30px" -->
    <!-- mycustomapp:title="xml zhong de text" /> -->

    <com.test.customviewtest.MyLinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        mycustomapp:Orientation="Vertical"
        mycustomapp:Text="this is custom LinearLayout" >
    </com.test.customviewtest.MyLinearLayout>

</RelativeLayout>


4. MainActivity.java

 

package com.test.customviewtest;

import android.app.Activity;
import android.os.Bundle;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	}
}


内容很简单,就不作详细介绍了,看一下demo基本就会搞了!!

posted @ 2013-05-02 21:24  行走的思想  阅读(45)  评论(0编辑  收藏  举报  来源