android自定义LinearLayout和View
自定义线性布局经常用到:
第一种是在扩展的LinearLayout构造函数中使用Inflater加载一个布局,并从中提取出相关的UI组件进行封装,形成一个独立的控件。在使用该控件时,由于它所有的子元素都是在运行时通过代码动态创建的,所以该控件只能以一个独立控件的形式在Layout文件中声明,例如:
public class CustomLayout extends LinearLayout{public CustomLayout(Context context){LayoutInflater mInflater = LayoutInflater.from(context);View myView = mInflater.inflate(R.layout.receive, null);addView(myView);}
}
< LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="wrap_content"android:layout_height="wrap_content"androidundefinedrientation="vertical" ><LinearLayout android:layout_width="wrap_content"android:layout_height="wrap_content"androidundefinedrientation="horizontal"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content" /><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content" android:id="@+id/button" /></LinearLayout><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content" /> < /LinearLayout>
实例:
imagebtn.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" ><ImageViewandroid:id="@+id/imageView1"android:layout_width="46dp"android:layout_height="46dp"android:layout_gravity="center_vertical"android:layout_marginRight="10dp"android:src="@drawable/confirm" /><TextViewandroid:id="@+id/textView1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center_vertical"android:text="确定"android:textSize="25dp" /></LinearLayout>
MyLinearLayout1.java
package com.hust.customlinearlayout;import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;public class MyLinearLayout1 extends LinearLayout {private ImageView imageView;private TextView textView;public MyLinearLayout1(Context context){super(context);}public MyLinearLayout1(Context context, AttributeSet attrs) {super(context, attrs); LayoutInflater inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);inflater.inflate(R.layout.imagebtn, this);imageView=(ImageView) findViewById(R.id.imageView1);textView=(TextView)findViewById(R.id.textView1); }public void setImageResource(int resId){imageView.setImageResource(resId);}public void setTextViewText(String text){textView.setText(text);}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="wrap_content"android:gravity="center_horizontal"android:orientation="horizontal" ><com.hust.customlinearlayout.MyLinearLayout1android:id="@+id/btn_right"android:layout_height="wrap_content" android:layout_width="wrap_content"android:layout_weight="1"/><com.hust.customlinearlayout.MyLinearLayout1android:id="@+id/btn_error"android:layout_marginLeft="5dp"android:layout_weight="1"android:layout_height="wrap_content" android:layout_width="wrap_content"/></LinearLayout>
package com.hust.customlinearlayout;import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;public class MainActivity extends ActionBarActivity {private MyLinearLayout1 myLinearLayout1;private MyLinearLayout1 myLinearLayout2;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);myLinearLayout1=(MyLinearLayout1) findViewById(R.id.btn_right);myLinearLayout2=(MyLinearLayout1) findViewById(R.id.btn_error);myLinearLayout1.setTextViewText("确定");myLinearLayout2.setTextViewText("取消");myLinearLayout1.setImageResource(R.drawable.confirm);myLinearLayout2.setImageResource(R.drawable.cancle);myLinearLayout1.setOnClickListener(new View.OnClickListener() {public void onClick(View v) {// TODO Auto-generated method stubToast.makeText(getApplicationContext(), "点击的正确按钮", 1).show();}});myLinearLayout2.setOnClickListener(new View.OnClickListener() {public void onClick(View v) {// TODO Auto-generated method stubToast.makeText(getApplicationContext(), "点击的错误按钮", 1).show();}});}}
第二种方式是:这个自定义VIEW中的任何控件都不是通过XML文件来定义的,而是在JAVA代码中通过动态生成的,然后再addView()加入到你自定义的View中,
private class SpeechView extends LinearLayout { private TextView mTitle; private TextView mDialogue; public SpeechView(Context context, String title, String words) { super(context); this.setOrientation(VERTICAL); // Here we build the child views in code. They could also have // been specified in an XML file.mTitle = new TextView(context);mTitle.setText(title); addView(mTitle, new LinearLayout.LayoutParams( LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));mDialogue = new TextView(context); mDialogue.setText(words); addView(mDialogue, new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));} /** * Convenience method to set the title of a SpeechView*/ public void setTitle(String title) { mTitle.setText(title); } /*** Convenience method to set the dialogue of a SpeechView */ public void setDialogue(String words) {mDialogue.setText(words); } }
android自定义LinearLayout和View相关推荐
- Android 自定义评论回复view
先上效果图: 一.创建xml 1.android_ceshi_activity <?xml version="1.0" encoding="utf-8"? ...
- Android 自定义LinearLayout实现滑动下拉抽屉的功能
先看效果图: 先来说说思路:我们把该页面分为两部分,分别是头部的抽屉布局(海洋色背景)和主内容布局(白色背景),这两部分的布局是呈线性关系,即抽屉在上,主页面在下,并且它们的父布局应该是一个可滑动的L ...
- android linearlayout 自定义,android 自定义LinearLayout
接着上一篇,本篇玩一下自定义LinearLayout,直接上代码: 1. attr.xml 2.继承LinearLayout重新定义一个: package com.test.customviewtes ...
- android自定义验证码倒计时View
关于自定义View的构造方法里面的参数的含义可以参考: http://www.cnblogs.com/angeldevil/p/3479431.html 代码: 倒计时类: public class ...
- android自定义空的view,ListView android中的自定义空视图
如果ListView适配器中没有数据,我想显示刷新Button和TextView.我还希望能够向将重新加载列表的按钮添加单击侦听器.以下是我定义当前活动的方式: protected void onCr ...
- Android 自定义手势解锁View
直接上代码了: /****@ClassName:GraphicsView*@author:WYL*@Date:2022/9/29*/ class GraphicsView : View {privat ...
- android 前台服务自定义布局不显示_Android自定义LinearLayout布局显示不完整的解决方法...
发现问题 原需求,在一个伸缩列表中,自定义LinearLayout继承LinearLayout动态添加布局. 然而实现的时候:一共遍历了30条数据,却只显示了一条 断点查看代码:遍历addView() ...
- Android 自定义View课程表表格
自己闲下来时间写的一个课表控件使用的自定义LinearLayout 里面View都是用代码实现的 最终效果如下图 写的可能有问题希望多多指点 创建一个自定义LinearLayout 控件用来装载课 ...
- Android自定义View课程表,Android 自定义View课程表表格
自己闲下来时间写的一个课表控件使用的自定义LinearLayout 里面View都是用代码实现的 最终效果如下图 写的可能有问题希望多多指点 创建一个自定义LinearLayout 控件用来装载课程的 ...
最新文章
- 计算机组成 试题,计算机组成典型试题及答案
- SubVersion和Subclipse的简单使用方法
- mysql date week_mysql weekday(date)/subdate(date,间隔天数)查询年龄/本月/周过生日
- 小猿圈Web前端开发学习路线
- Remove Duplicates from Sorted List 去除链表中重复值节点
- 常见设计模式 (python代码实现)
- android国籍组件,android组件化之路
- Codeforces Round #452 (Div. 2)
- 整理struct sockaddr和struct sockaddr_in
- 从程序员到项目经理(24):慎于问敏于行 - 忠于工作不等于奴性
- 基于Centos搭建Maven 安装与使用
- 写失败数据写入成功_深度 | 缓存与数据库一致性问题剖析
- STM32 高速定时器配置为PWM使用细节
- 计算机操作入门基础知识,电脑入门基本操作知识
- nodejs对PDF合并的几种方法
- WPS表格怎么画横线并在上面打字
- 第一世界的年轻人追求的是房子还是别的?
- python读取文本两个数字的成语_只要2步!将搜狗词库(scel)转为Python可读的文本...
- linux打印机测试程序,轻松了解Linux打印之CUPS软件
- 怎么把ofd转换成PDF文件?分享给你个好用的方法。