第三篇--复合控件的两种方式
来源:互联网 发布:淘宝双十一退货潮 编辑:程序博客网 时间:2024/05/01 11:12
第一种:
1.创建子类,继承layout
2.创建xml,自定义的layout进行填充
ImageBtn.java
package com.godlovesoccer.blogcustomlayout;import android.content.Context;import android.util.AttributeSet;import android.view.LayoutInflater;import android.widget.LinearLayout;import android.widget.ImageView;import android.widget.TextView;public class ImageBtn extends LinearLayout{private ImageView imageView;private TextView textView;public ImageBtn(Context context){super(context);}public ImageBtn(Context context,AttributeSet attrs){super(context,attrs);LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);inflater.inflate(R.layout.customlayout, 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);}}customlayout.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:background="#FF0000FF" android:orientation="horizontal" > <ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:paddingBottom="5dp" android:paddingLeft="40dp" android:paddingTop="5dp" android:src="@drawable/ic_launcher" /> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_margin="8dp" android:text="确定" android:textColor="#000000" /></LinearLayout>
这样就自定义了一个View(其实是ViewGroup,继承LinearLayout)
在main.xml中进行布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/LinearLayout1" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.godlovesoccer.blogcustomlayout.MainActivity" > <com.godlovesoccer.blogcustomlayout.ImageBtn android:id="@+id/btn_rigth" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="1dp" > </com.godlovesoccer.blogcustomlayout.ImageBtn> <com.godlovesoccer.blogcustomlayout.ImageBtn android:id="@+id/btn_err" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="1dp" android:background="#00EE11" > </com.godlovesoccer.blogcustomlayout.ImageBtn></LinearLayout>在mainactivity.java中进行填充主main.xml
package com.godlovesoccer.blogcustomlayout;import android.support.v7.app.ActionBarActivity;import android.os.Bundle;public class MainActivity extends ActionBarActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);ImageBtn imageBtn1=(ImageBtn)findViewById(R.id.btn_rigth);ImageBtn imageBtn2=(ImageBtn)findViewById(R.id.btn_err);imageBtn1.setTextViewText("确定");imageBtn2.setTextViewText("取消");}}
第二种,代码定义
0 0
- 第三篇--复合控件的两种方式
- symbian 3rd/5th 复合控件的两种实现方式
- hibernate复合主键的两种方式(1)
- 两种复合数列排序方式
- 控件子类化的两种方式
- 两种方式配置Hibernate复合主键(修正)
- Java线程第三课:线程的两种创建方式
- Android集成第三方app的两种方式
- C#多线程操作控件的两种安全方式(委托方式和BackgroundWorker控件方式)
- 复合控件的开发
- 复合控件的开发
- 复合控件的开发
- 控件的字体设置的两种方式
- 检测控件是否安装的两种方式:
- 全屏模式下处理Silverlight控件的两种方式
- 检测控件是否安装的两种方式:
- 移除子控件两种方式的区别
- 在onCreate中获取控件大小的两种方式
- 一致性哈希
- IT忍者神龟之Oracle查询树形结构
- 介绍个超好用的切割数组的函数--array_slice
- XML编程经验
- Cocos2dx 学习笔记24 CCLabelTTF ,CCLabelBMFont,CCLabelAtlas
- 第三篇--复合控件的两种方式
- 计算广告学(Computational Advertising)概述学习
- C#根据控件名获取控件对象
- 安装CentOS 6.5系统【详细图文教程】
- 根据linux内核源码查找recv返回EBADF(errno 9)的原因
- 加描点
- 搜索广告排序与定价
- 基于系统工程的软件工程在C#下搭建的技术体系研究
- HI3516A 高清4M支持H265/HEVC