自定义View的几种写法一

(一)组合控件

 1、就是通过将一些简单的控件组合在一起,在另一个布局文件中通过其类名全称,作为一个控件使用,并且要写个类去加载这个组合布局,这个类要继承自它的Viewgroup,实现构造方法。

二、例子:

1、效果图


2、先写一个布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >
<RelativeLayout 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#000"
    >
    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:textColor="#fff"
        android:text="微信"
        android:textSize="22sp" />


    <ImageButton
        android:id="@+id/ibAdd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:src="@drawable/jia" />


    <ImageButton
        android:id="@+id/ibSearch"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginRight="48dp"
        android:layout_toLeftOf="@+id/ibAdd"
        android:src="@drawable/seach" />
</RelativeLayout>
</RelativeLayout>

2、写个类去加载这个布局文件,这个类继承它的Viewgroup



public class TitleActivity extends RelativeLayout {




public TitleActivity(Context context) {
super(context);
// 加载布局
LayoutInflater.from(context).inflate(R.layout.title, this);

}


public TitleActivity(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
LayoutInflater.from(context).inflate(R.layout.title, this);
}


public TitleActivity(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater.from(context).inflate(R.layout.title, this);
}



}

3、使用自定义布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >
    
<com.example.myview.TitleActivity
    android:id="@+id/titleBar"
     android:layout_width="match_parent"
    android:layout_height="wrap_content"
    ></com.example.myview.TitleActivity>
</RelativeLayout>

4、加载使用自定义布局的布局

package com.example.myview;


import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends Activity {


private ImageButton search;
private ImageButton add;
private TextView title;


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

search = (ImageButton) findViewById(R.id.ibSearch);
add = (ImageButton) findViewById(R.id.ibAdd);
title = (TextView) findViewById(R.id.title);
add.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "你点击了一下", 0).show();
}
});
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值