您的位置:首页 > 移动开发 > Android开发

Android进阶之路 - 自定义控件的简单使用(一)

2017-01-08 19:13 316 查看
本篇带给大家的是include的简单使用,还有初级自定义控件的使用方式

关于Include的使用方式,大家可以通过下面链接进行学习:

http://blog.csdn.net/qq_20451879/article/details/54288317

UI结构:

1.include视图的复用

2.自定义控件事件响应

3.最基本的自定义控件引用

目录:

1.MainActivity与TitleCostom、CostomLayout代码

2.各自的Xml布局

3.注意点(可能会帮你更快速的熟练使用)

MainActivity:

package com.example.costomdemo;

import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.Activity;

@SuppressLint("NewApi")
public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//去掉ActinBar,也可以自己在清单配置
ActionBar actionBar = getActionBar();
if (actionBar!=null) {
actionBar.hide();
}
}
}


CostomLayout(无事件响应):

package com.example.costomdemo;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.FrameLayout;

/**
* 最简单的自定义控件
* */
public class CostomLayout extends FrameLayout{

public CostomLayout(Context context, AttributeSet attrs) {
super(context, attrs);
//这里其实要要注意的还是参数问题,指的是Inflate的第二个参数,它属于ViewGroup,这个时候我们不可以像其实时候填充为null,而应该指出是当前的布局,所以添为上下文
LayoutInflater.from(context).inflate(R.layout.costom_xml, this);
}

}


TitleCostom(事件响应):

package com.example.costomdemo;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;

/**
* 自定义控件的事件处理
* */
public class TitleCostom extends LinearLayout {

public TitleCostom(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater.from(context).inflate(R.layout.title_xml, this);
Button mButton = (Button) findViewById(R.id.title_costom);
mButton.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
Toast.makeText(getContext(), "自定义控件入门使用", 0).show();
}
});
}
}


MainActivity的Xml:

<LinearLayout 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"
android:orientation="vertical"
>
<include
layout="@layout/include_xml"
android:layout_width="match_parent"
android:layout_height="20dp"
/>
<com.example.costomdemo.TitleCostom
android:layout_width="match_parent"
android:layout_height="30dp"
/>

<com.example.costomdemo.CostomLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
/>
</LinearLayout>


CostomLayout的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:orientation="vertical" >

<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Costom初级页面"
android:gravity="center"
/>

</LinearLayout>
<
4000
/pre>

TitleCostom的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:orientation="horizontal" >

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="自定义入门"
android:textSize="10sp"
android:id="@+id/title_costom"
android:gravity="center"
/>

</LinearLayout>


include复用的layout:

<?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:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Include简单的视图复用"
android:textSize="10sp"
android:gravity="center"
/>
</LinearLayout>


注意点:

1、首先在Xml中使用include标签,需要有对应的layout,不需要绑定对应的Activity、Fragment等,但是无法做事件响应这是一个缺点,不过复用性比较可观,如果要单独事件响应,需要设置id,这个大家可以跳转我们上面的链接进行了解

2、使用自定义控件一定要记得继承(extends)Layout

3、布局填充LayoutInflater.from(context).inflate(R.layout.costom_xml, this);第二个参数是Viewgrop所以一定注意

4、在对应布局引用的时候一定记得copy对应的包名,类名,因为它本身已经被我们定义为一个控件,好比你使用TextView之类的是一样的,为了提醒你,send you a photo;

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