我们所有的控件都是继承至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. codeforces 342D Xenia and Dominoes(状压dp+容斥)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud D. Xenia and Dominoes Xenia likes puzzles ...

  2. QT TCP/IP

    QT 网络通信(TCP/IP) 服务端: 一.监听新的客户端接入(QTcpServer) 重写函数 incomingConnection(qintptr socketDescriptor) 二.服务端 ...

  3. git rm

    git rm可以执行删除的条件(前提是要被删除的文件还没有从workspace tree中移除) git rm可以执行删除的条件:被删除文件在index当中的blob内容必须与HEAD commit指 ...

  4. 关于PS里图层样式的全局光

    勾选“使用全局光”,则各个图层样式的光源角度都会相同. 我在“内阴影”效果里勾选了“使用全局光”,然后我发现当我在“投影”效果和“斜面和浮雕”效果里选择使用全局光时,它们的光源角度自动变成120度, ...

  5. window7下statsvn统计代码量

    下载statsvn:http://www.statsvn.org/ 将下载后的statsvn.jar放到d:\svn目录下; 打开cmd窗口切换到需要统计代码的项目目录如:d:\project\web ...

  6. css3的loadding效果

    <!DOCTYPE html> <html> <head> <title>CSS3 loading效果</title> <meta c ...

  7. OpenSSL “心脏滴血”漏洞

    OpenSSL "心脏滴血"漏洞 漏洞描述 : OpenSSL软件存在"心脏出血"漏洞,该漏洞使攻击者能够从内存中读取多达64 KB的数据,造成信息泄露. 漏洞 ...

  8. HDU 5775 Bubble Sort(线段树)(2016 Multi-University Training Contest 4 1012)

    原址地址:http://ibupu.link/?id=31 Problem Description P is a permutation of the integers from 1 to N(ind ...

  9. 几个简单的css设置问题:div居中,ul li不换行 ,内容超出自动变省略号等

    1  div在页面居中的问题 1)position值为relative时(相对定位),css设置属性margin:0 auto;(0 auto,表示上下边界为0,左右则根据宽度自适应相同值,即居中)即 ...

  10. html&CSS初学

    <link href="https://fonts.gdgdocs.org/css?family=Lobster" rel="stylesheet" ty ...