我们所有的控件都是继承至View类的,而所有的布局都是继承至ViewGroup的,所以我们也可以继承某个view类来实现我们自己的布局或者控件.

  • 引入布局

    我们新建一个title.xml的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="horizontal" >
<Button
android:id="@+id/btn_back"
android:layout_width="80dp"
android:layout_height="40dp"
android:text="back" /> <TextView
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="1"
android:gravity="center"
android:text="title"
android:textSize="24sp" /> <Button
android:id="@+id/btn_next"
android:layout_width="80dp"
android:layout_height="40dp"
android:text="next" />
</LinearLayout>

然后在mian_activity.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="wrap_content"
android:orientation="vertical"
tools:context="com.example.alertdialog.MainActivity" > <!-- 引用title.xml文件 -->
<include layout="@layout/title"/>
<Button
android:id="@+id/btn"
android:layout_width="match_parent"
android:layout_height="40dp"
/>
</LinearLayout>
  • 创建自定义控件

    新建一个titleLayout的class文件,让他继承至LinearLayout类
package com.example.alertdialog;
import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast; public class TitleLayout extends LinearLayout{ public TitleLayout(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater.from(context).inflate(R.layout.title, this);
findViewById(R.id.btn_back).setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
((Activity)getContext()).finish(); //获取到当前正在活动的activity,然后finish掉.
}
});
findViewById(R.id.btn_next).setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
Toast.makeText(getContext(), "next", Toast.LENGTH_SHORT).show();
}
});
}
}

然后我们可以在layout文件中字节通过类的路径来进行引用.

<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="wrap_content"
android:orientation="vertical"
tools:context="com.example.alertdialog.MainActivity" > <com.example.alertdialog.TitleLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
></com.example.alertdialog.TitleLayout>
<Button
android:id="@+id/btn"
android:layout_width="match_parent"
android:layout_height="40dp"
/>
</LinearLayout>

Android 创建自定义布局的更多相关文章

  1. Android创建自定义的布局和控件

    Android的自带布局有framelayout.linerlayout.relativelayout,外加两个百分比布局,但是这些无法灵活的满足我们的需要,所以我们要自己自定义并引入自己的布局.首先 ...

  2. Android创建自定义dialog方法详解-样式去掉阴影效果

    在自定义组件时,从已有组件源码中会很大收获.就拿progressDialog来说     间接父类是dialog,想了解dialog继承结构可以去百度,或者    从构造器来说ProgressDial ...

  3. 【转】Android AlertDialog自定义布局

    原文网址:https://blog.csdn.net/u010694658/article/details/53022294 由于开发中经常使用弹框,然而系统自带的弹框太局限,也不太美观,经常不能满足 ...

  4. android创建自定义对话框

    创建如下自定义对话框: JAVA代码 LayoutInflater li = LayoutInflater.from(TagActivity. this);  //NOTE final View Te ...

  5. 【翻译】在Ext JS和Sencha Touch中创建自定义布局

    原文:Creating Custom Layouts in Ext JS and Sencha Touch 布局系统是Sencha框架中最强大和最独特的一部分.布局会处理应用程序中每个组件的大小和位置 ...

  6. Android创建自定义Application

    开发目的 创建一个自定义的MainApplication继承Application. 读取AndroidManifest.xml文件中Application节点的META-DATA数据.此处以ApiK ...

  7. Android 创建自定义 View 的属性 (attrs) 时需要注意的问题

    自定义 View 的属性并不难,可以参照官方的文档 https://developer.android.com/training/custom-views/create-view.html 但是需要注 ...

  8. Android:创建可穿戴应用 - 自定义布局

    创建自定义布局(Creating Custom Layouts) 本文将介绍如何创建自定义通知以及使用可穿戴UI库来创建自定义布局你同时还需要了解可穿戴设计准则(Wear Design Princip ...

  9. android_自定义布局例子

    为什么要写自定义布局: 1.在实现大量重复的子按键或者子布局时,如果一个一个去复写工作量庞大,就需要创建自定义布局直接导入布局里,可以节省大量的时间 创建自定义布局的步骤: 1.编写一个自定义xml布 ...

随机推荐

  1. C语言函数入门

    由于采用了函数模块式的结构,C语言易于实现结构化程序设计.使程序的层次结构清晰,便于程序的编写.阅读.调试. main 函数是主函数,它可以调用其它函数,而不允许被其它函数调用.因此,C程序的执行总是 ...

  2. 裸kmp算法

    Number Sequence Problem Description Given two sequences of numbers : a[1], a[2], ...... , a[N], and ...

  3. PHP结合Linux的cron命令实现定时任务

    PHP死循环 来处理定时任务的效率是很低的.(众多网友评价)大家都建议使用Linux内置的定时任务crontab命令来调用php脚本来实现. PHP定时任务的两种方法:1.web方式调用php网页,但 ...

  4. 编译安装zabbix

    1. 说明:本例用源码包来安装zabbix,但是zabbix的后台数据库在这里选择mysql,然而mysql的安装方式不在选择源码了,而是选择已经编译好的通用linux包(tar包) zabbix源码 ...

  5. 【待整理】IOS开发之下载

    一.传统的下载文件的方式 - (void)downloaderWithUrl:(NSURL *)url { NSURLRequest *request = [NSURLRequest requestW ...

  6. WPF实现毛玻璃效果

    1和2需要Microsoft.WindowsAPICodePack.Shell.dll 和引用using System.Windows.Interop,并只能在有DwmApi.dll 版本的Windo ...

  7. Windows7里的“计算器”你真的会用吗?

    “计算器”是不同Windows版本中的必备工具,虽然功能单一,但的确是人们日常工作中不可缺少的辅助工具,本文就来谈谈它的使用. 一.标准型和科学型两种面板 我们既可从Windows附件菜单中启动它,也 ...

  8. SqlDataReader类

    一.常用属性 名称 说明 Depth 获取一个值,用于指示当前行的嵌套深度.  FieldCount 获取当前行中的列数. HasRows 获取一个值,该值指示 SqlDataReader 是否有行. ...

  9. WPF笔记(1.2 Navigation导航)——Hello,WPF!

    原文:WPF笔记(1.2 Navigation导航)--Hello,WPF! 这一节是讲导航的.看了一遍,发现多不能实现,因为版本更新了,所以很多旧的语法不支持了,比如说,不再有NavigationA ...

  10. C语言的本质(35)——共享库

    库用于将相似函数打包在一个单元中.然后这些单元就可为其他开发人员所共享,并因此有了模块化编程这种说法- 即,从模块中构建程序.Linux支持两种类型的库,每一种库都有各自的优缺点.静态库包含在编译时静 ...