Android中自定义View可以分两种,一种为使用系统提供View来拼成一个想要的View(比如在程序中多次出现同样的view,可以共用,相当于封装吧),另一种为自己直接使用Paint画。
本文是属于第一种,大概可分4个步骤,如果不需要自定义标签属性第一步则可跳过。
代码如下:
1. 写一个attrs:(如果不需要自定义属性则不需要)
<declare-styleable name="MyLayoutView"> <attr format="reference" name="tile" /> <attr format="reference" name="tittle" /> <attr format="reference" name="content" /> </declare-styleable> 2. layout布局(my_layout.xml):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center_vertical"> <RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" > <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_toLeftOf="@+id/my_tile" android:layout_marginLeft="15dip" android:orientation="vertical" > <TextView android:id="@+id/my_tittle" style="@style/T7_2_Subhead" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="15dip" android:includeFontPadding="false" android:text="" /> <TextView android:id="@+id/my_description" style="@style/T7_5_Bodycopy" android:layout_width="wrap_content" android:layout_height="wrap_content" android:includeFontPadding="false" /> </LinearLayout> <TextView android:id="@+id/my_tile" android:layout_width="200dip" android:layout_height="200dip" android:layout_alignParentRight="true" android:scaleType="fitXY" /> </RelativeLayout> </LinearLayout> 3. 编写MyLayoutView: packge com.a.b; public class MyLayoutView extends LinearLayout { private int tile; private int tittle; private int content; private ImageView tileView; private TextView tittleView; private TextView contentView; private View view; public MyLayoutView(Context context) { super(context); } public MyLayoutView(Context context, AttributeSet attrs) { super(context, attrs); TypedArray array = context.obtainStyledAttributes(attrs,R.styleable.MyLayoutView); tile = array.getResourceId(R.styleable.MyLayoutView_tile, -1); tittle = array.getResourceId(R.styleable.MyLayoutView_tittle, -1); content = array.getResourceId(R.styleable.MyLayoutView_content, -1); LayoutInflater inflater = LayoutInflater.from(context); this.view = inflater.inflate(R.layout.my_layout, null); tileView = (ImageView) view.findViewById(R.id.my_tile); tittleView = (TextView) view.findViewById(R.id.my_tittle); contentView = (TextView) view.findViewById(R.id.my_description); } @Override protected void onFinishInflate() { super.onFinishInflate(); this.initialize(); } private void initialize() { tileView.setImageResource(tile); tittleView.setText(tittle); contentView.setText(content); LinearLayout.LayoutParams activityParams = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT); this.removeView(this.view); this.addView(this.view, activityParams); } } 4. 使用:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:my="http://schemas.android.com/apk/res/com.a.b" android:layout_width="fill_parent" android:layout_height="fill_parent" > <com.a.b.MyLayoutView android:layout_width="fill_parent" android:layout_height="fill_parent" my:content="@string/my_summary1" my:tile="@drawablemy_one_image" my:tittle="@string/my_tittle1" /> </RelativeLayout> |