第三篇--复合控件的两种方式

来源:互联网 发布:淘宝双十一退货潮 编辑:程序博客网 时间:2024/05/01 11:12

第一种:

1.创建子类,继承layout

2.创建xml,自定义的layout进行填充

ImageBtn.java

package com.godlovesoccer.blogcustomlayout;import android.content.Context;import android.util.AttributeSet;import android.view.LayoutInflater;import android.widget.LinearLayout;import android.widget.ImageView;import android.widget.TextView;public class ImageBtn extends LinearLayout{private ImageView imageView;private TextView textView;public ImageBtn(Context context){super(context);}public ImageBtn(Context context,AttributeSet attrs){super(context,attrs);LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);inflater.inflate(R.layout.customlayout, this);imageView=(ImageView)findViewById(R.id.imageView1);textView=(TextView)findViewById(R.id.textView1);}public void setImageResource(int resId){imageView.setImageResource(resId);}public void setTextViewText(String text){textView.setText(text);}}
customlayout.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="#FF0000FF"    android:orientation="horizontal" >    <ImageView        android:id="@+id/imageView1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="center_vertical"        android:paddingBottom="5dp"        android:paddingLeft="40dp"        android:paddingTop="5dp"        android:src="@drawable/ic_launcher" />    <TextView        android:id="@+id/textView1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="center_vertical"        android:layout_margin="8dp"        android:text="确定"        android:textColor="#000000" /></LinearLayout>

这样就自定义了一个View(其实是ViewGroup,继承LinearLayout)

在main.xml中进行布局:

        

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/LinearLayout1"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context="com.godlovesoccer.blogcustomlayout.MainActivity" >    <com.godlovesoccer.blogcustomlayout.ImageBtn        android:id="@+id/btn_rigth"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_margin="1dp" >    </com.godlovesoccer.blogcustomlayout.ImageBtn>    <com.godlovesoccer.blogcustomlayout.ImageBtn        android:id="@+id/btn_err"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_margin="1dp"        android:background="#00EE11" >    </com.godlovesoccer.blogcustomlayout.ImageBtn></LinearLayout>
在mainactivity.java中进行填充主main.xml

package com.godlovesoccer.blogcustomlayout;import android.support.v7.app.ActionBarActivity;import android.os.Bundle;public class MainActivity extends ActionBarActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);ImageBtn imageBtn1=(ImageBtn)findViewById(R.id.btn_rigth);ImageBtn imageBtn2=(ImageBtn)findViewById(R.id.btn_err);imageBtn1.setTextViewText("确定");imageBtn2.setTextViewText("取消");}}

第二种,代码定义


0 0
原创粉丝点击