自定义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();
}
});
}
}
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();
}
});
}
}
相关文章推荐
- 自定义Mapper文件的几种写法
- Android中利用ViewHolder优化自定义Adapter的写法(必看)
- 几种UITableview自定义Cell的办法
- 利用ViewHolder优化自定义Adapter的典型写法
- 自定义View的几种方法
- 利用ViewHolder优化自定义Adapter的典型写法
- 几种UITableview自定义Cell的办法
- Android-View自定义属性-简化写法
- 自定义View典型写法
- Lightswitch C1FLexGrid 自定义列显示格式的几种写法
- android自定义view的几种方式
- Android自定义View之三种流行进度条的写法
- swift 自定义view的写法(内有仿照OC中block的 swift闭包的调用)
- 向界面中加入自定义View的几种方式
- 记录自定义View的几种方式
- 给tableView 的cell赋值的几种写法
- 利用ViewHolder优化自定义Adapter的典型写法
- Android自定义View实现的几种方式
- 安卓中自定义view控件代替radiogroup实现颜色渐变效果的写法
- swift 自定义view的写法(内有仿照OC中block的 swift闭包的调用)