您的位置:首页 > 其它

自定义View的几种写法一

2017-05-11 10:42 204 查看
(一)组合控件

 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();

}
});
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: