我们所有的控件都是继承至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# ?? 运算符,不能忘记的知识点

    最近项目中有一个bug被测试(是黑盒测试)发现了,跟了老半天代码,才找到这个问题的所在,原来是一个计算表达式中用到了??运算符,才导致了这个错误,下面让我简单讲述一下. C# ?? 运算符 msdn上 ...

  2. 第二章IPC——IPC与开启多进程

    问题 一.IPC ①.什么是IPC  ②.为什么要有IPC 二.多进程 ①.如何开启多进程  ②.系统如何创建多进程 三.多进程引发的问题 问:私有进程(利用":+进程名")能否共 ...

  3. php sscanf() 函数使用

    定义和用法 sscanf() 函数根据指定的格式解析来自一个字符串的输入. 如果只向该函数传递两个参数,数据将以数组的形式返回.否则,如果传递了额外的参数,那么被解析的数据会存储在这些参数中.如果区分 ...

  4. python运维开发(二十二)---JSONP、瀑布流、组合搜索、多级评论、tornado框架简介

    内容目录: JSONP应用 瀑布流布局 组合搜索 多级评论 tornado框架简介 JSONP应用 由于浏览器存在同源策略机制,同源策略阻止从一个源加载的文档或脚本获取或设置另一个源加载的文档的属性. ...

  5. python笔记之ZipFile模块

    python笔记之ZipFile模块 zipfile模块用来做zip格式编码的压缩和解压缩的,zipfile里有两个非常重要的class, 分别是ZipFile和ZipInfo, 在绝大多数的情况下, ...

  6. opencv for python

    opencv显示图像: # -*- coding: UTF-8 -*- import numpy as np import cv2 from matplotlib import pyplot as p ...

  7. POJ 1564 Sum It Up(DFS)

    Sum It Up Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit St ...

  8. .net项目IIS、VS 附加进程调试

    IIS调试 1.首先要把项目发布至IIS上,确保项目能正常运行. 2.从IIS上右键站点>管理网站>浏览 或者打开“内容视图“ 选择一个文件右键>浏览. 3.用vs打开该项目,选择 ...

  9. 安卓手机用-----Exchange Activesync---同步Gmail日历联系人

    微软Exchange Activesync是一个跨平台的移动设备同步协议 支持很多手机操作系统,比如诺基亚的塞班.黑莓的palm.WindowsMobile.Iphone.安卓等.这些手机上都可以用这 ...

  10. Number of Containers(数学) 分类: 数学 2015-07-07 23:42 1人阅读 评论(0) 收藏

    Number of Containers Time Limit: 1 Second Memory Limit: 32768 KB For two integers m and k, k is said ...