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

  • 引入布局

    我们新建一个title.xml的layout文件.
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="horizontal" >
  6. <Button
  7. android:id="@+id/btn_back"
  8. android:layout_width="80dp"
  9. android:layout_height="40dp"
  10. android:text="back" />
  11. <TextView
  12. android:layout_width="0dp"
  13. android:layout_height="40dp"
  14. android:layout_weight="1"
  15. android:gravity="center"
  16. android:text="title"
  17. android:textSize="24sp" />
  18. <Button
  19. android:id="@+id/btn_next"
  20. android:layout_width="80dp"
  21. android:layout_height="40dp"
  22. android:text="next" />
  23. </LinearLayout>

然后在mian_activity.xml文件中引用.

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="wrap_content"
  5. android:orientation="vertical"
  6. tools:context="com.example.alertdialog.MainActivity" >
  7. <!-- 引用title.xml文件 -->
  8. <include layout="@layout/title"/>
  9. <Button
  10. android:id="@+id/btn"
  11. android:layout_width="match_parent"
  12. android:layout_height="40dp"
  13. />
  14. </LinearLayout>
  • 创建自定义控件

    新建一个titleLayout的class文件,让他继承至LinearLayout类
  1. package com.example.alertdialog;
  2. import android.app.Activity;
  3. import android.content.Context;
  4. import android.util.AttributeSet;
  5. import android.view.LayoutInflater;
  6. import android.view.View;
  7. import android.widget.LinearLayout;
  8. import android.widget.Toast;
  9. public class TitleLayout extends LinearLayout{
  10. public TitleLayout(Context context, AttributeSet attrs) {
  11. super(context, attrs);
  12. LayoutInflater.from(context).inflate(R.layout.title, this);
  13. findViewById(R.id.btn_back).setOnClickListener(new OnClickListener() {
  14. @Override
  15. public void onClick(View v) {
  16. ((Activity)getContext()).finish(); //获取到当前正在活动的activity,然后finish掉.
  17. }
  18. });
  19. findViewById(R.id.btn_next).setOnClickListener(new OnClickListener() {
  20. @Override
  21. public void onClick(View v) {
  22. Toast.makeText(getContext(), "next", Toast.LENGTH_SHORT).show();
  23. }
  24. });
  25. }
  26. }

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

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="wrap_content"
  5. android:orientation="vertical"
  6. tools:context="com.example.alertdialog.MainActivity" >
  7. <com.example.alertdialog.TitleLayout
  8. android:layout_width="match_parent"
  9. android:layout_height="wrap_content"
  10. ></com.example.alertdialog.TitleLayout>
  11. <Button
  12. android:id="@+id/btn"
  13. android:layout_width="match_parent"
  14. android:layout_height="40dp"
  15. />
  16. </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. 写一个Windows上的守护进程(6)Windows服务

    写一个Windows上的守护进程(6)Windows服务 守护进程因为要开机启动,还要高权限,所以我就把它做成Windows服务了. 关于Windows服务的官方文档,大家可以看https://msd ...

  2. mysql中函数DISTINCT,group by,CONCAT及GROUP_CONCAT的使用

    一:DISTINCT 在使用mysql时,有时需要查询出某个字段不重复的记录,虽然mysql提供有distinct这个关键字来过滤掉多余的重复记录只保留一条,但往往只用它来返回不重复记录的条数,而不是 ...

  3. python 多线程、多进程

    一.首先说下多线程.多进程用途及异同点,另外还涉及到队列的,memcache.redis的操作等: 1.在python中,如果一个程序是IO密集的操作,使用多线程:运算密集的操作使用多进程. 但是,其 ...

  4. Starship Troopers(HDU 1011 树形DP)

    题意: 给定n个定点和m个士兵,n个定点最终构成一棵树,每个定点有一定x个bugs和y个value,每20个bug需要消耗一个士兵,不足20也消耗一个,然后最终收获y个value,只有父节点被占领后子 ...

  5. 无法添加sql server ER图

    Database diagram support objects cannot be installed because this database does not have a valid own ...

  6. Unix 环境高级编程 (APUE) 之 网络 IPC:套接字

    一起学 Unix 环境高级编程 (APUE) 之 网络 IPC:套接字 . . . . . 目录 (一) 一起学 Unix 环境高级编程 (APUE) 之 标准IO (二) 一起学 Unix 环境高级 ...

  7. QT---线程间通信(要先编译libqt-mt.so?)

    在 Qt 系统中,运行着一个GUI 主事件线程,这个主线程从窗口系统中获取事件,并将它们分发到各个组件去处理.在 QThread 类中有一种从非主事件线程中将事件提交给一个对象的方法,也就是 QThr ...

  8. 感觉tbceditor很不错,如果作者能坚持下来,非常非常看好啊

    感觉tbceditor很不错,如果作者能坚持下来,非常非常看好啊 你技术好,可以做个自用的IDE慢慢加功能 ,很方便的用这个控件,写个支持Delphi和html混编的编辑器,要不到2个小时

  9. ViewBag、ViewData和TempData使用方法、区别与联系

    一.区别与联系 ViewData 和 TempData 都可以传递弱类型数据,区别如下:TempData 只在当前 Action 中有效,生命周期和 View 相同:保存在Session中,Contr ...

  10. 配置文件入门 - WebConfig.config常用配置节点介绍

    一.配置文件入门 .Net提供了一种保存项目配置信息的办法,就是利用配置文件,配置文件的后缀一般是.config.在WinForm程序中配置文件一般是App.config.在Asp.net中一般默认是 ...