android 自定义LinearLayout
来源:互联网 发布:c语言break跳出if 编辑:程序博客网 时间:2024/05/24 06:27
接着上一篇,本篇玩一下自定义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=2for (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 {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}}
内容很简单,就不作详细介绍了,看一下demo基本就会搞了!!
- Android 自定义 LinearLayout
- Android 自定义 LinearLayout
- android 自定义LinearLayout
- android之【LinearLayout自定义View】
- Android自定义控件---继承Linearlayout
- android自定义LinearLayout和View
- Android - 自定义View Demo - LinearLayout
- android LinearLayout自定义高亮背景
- Android自定义组件之文武双全LinearLayout
- 【android自定义控件】LinearLayout定义ActionBar样式
- Android自定义LinearLayout的三种方法
- 自定义LinearLayout
- 自定义LinearLayout
- 自定义Linearlayout
- android LinearLayout
- android LinearLayout
- Android -- LinearLayout
- Android LinearLayout
- Hibernate缓存
- Linux串口编程
- patch打 补丁
- bbed获取oracle dbid(oracle十进制和十六进制转换)
- web.xml 中的listener、 filter、servlet 加载顺序及其详解
- android 自定义LinearLayout
- oracle 集合
- c++类型-const的使用
- CCS 3.3 操作C函数读写文件
- POJ 1922 Ride to School
- [mini2440]如何配置minicom
- POJ 1562 Oil Deposits
- 9.2图的存储结构 9.2.1 邻接矩阵存储方法
- java中“==”和equals方法有什么区别