android 1.6 launcher研究之自定义ViewGroup (转 2011.06.03(二)——— android 1.6 launcher研究之自定义ViewGroup )
2011.06.03(2)——— android 1.6 launcher研究之自定义ViewGroup
2011.06.03(2)——— android 1.6 launcher研究之自定义ViewGroup
1、用xml来作为ViewGroup里面的View
参考:http://www.eoeandroid.com/thread-30888-1-1.html
MyViewGroup.java
package com.lp; import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup; public class MyViewGroup extends ViewGroup {
public MyViewGroup(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
} public MyViewGroup(Context context, AttributeSet attrs) {
super(context, attrs);
} public MyViewGroup(Context context) {
super(context);
initializeView(context);
} @Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
System.out.println(getChildCount());
for(int i=0;i<getChildCount();i++){
View view = getChildAt(i);
view.setVisibility(View.VISIBLE); view.measure(r - l, b - t);
view.layout(l, t, r, b);
}
} private void initializeView(Context context) {
//LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LayoutInflater inflater = LayoutInflater.from(context);
View mycontrol = inflater.inflate(R.layout.myview, null);
addView(mycontrol);
} }
myviewgroup.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="800px"
android:layout_height="600px"
android:background="#F33603"
android:orientation="vertical">
<Button
android:text="my button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<Button
android:text="my button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<Button
android:text="my button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
MainActivity.java
package com.lp; import android.app.Activity;
import android.os.Bundle; public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new MyViewGroup(this));
}
}
针对那个网址里面的错误 只要加上view.measure(r - l, b - t);就可以里了
2、自己些子view
参考:http://soft-app.iteye.com/blog/924757
MyViewGroup2.java
package com.lp; import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button; public class MyViewGroup2 extends ViewGroup { public MyViewGroup2(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
} public MyViewGroup2(Context context, AttributeSet attrs) {
super(context, attrs);
} public MyViewGroup2(Context context) {
super(context);
initializeView(context);
} @Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
System.out.println(getChildCount());
for(int i=0;i<getChildCount();i++){
View view = getChildAt(i);
view.setVisibility(View.VISIBLE); view.measure(r - l, b - t);
view.layout(10+i*5, 20+i*10, view.getMeasuredWidth(), view
.getMeasuredHeight());
}
} private void initializeView(Context context) {
Button btn1 = new Button(context);
btn1.setText("按钮1");
addView(btn1);
Button btn2 = new Button(context);
btn2.setText("按钮2");
addView(btn2);
} }
MainActivity.java
package com.lp; import android.app.Activity;
import android.os.Bundle; public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new MyViewGroup2(this));
}
}
注意:
1、两者的initializeView()方法不同 前者是从xml中获取view;后者是自己编写view
2、注意onLayout()方法
xml时
view.layout(l, t, r, b);
自己写时:
view.layout(10+i*50, 20+i*50, view.getMeasuredWidth(), view
.getMeasuredHeight());
也就是说 xml时 布局已经在xml里面说明了
另: 自定义ViewGroup的使用
1、如上面的
MainActivity.java
package com.lp; import android.app.Activity;
import android.os.Bundle; public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new MyViewGroup2(this));
}
}
2、layout的方式
<?xml version="1.0" encoding="utf-8"?>
<com.lp.MyViewGroup2
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:text="aaa"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<Button
android:text="bbb"
android:layout_width="wrap_content"
android:layout_height="60dip"
/>
</com.lp.MyViewGroup2>
然后 MainActivity.java
package com.lp; import android.app.Activity;
import android.os.Bundle; public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
android 1.6 launcher研究之自定义ViewGroup (转 2011.06.03(二)——— android 1.6 launcher研究之自定义ViewGroup )的更多相关文章
- Android View 的绘制流程之 Layout 和 Draw 过程详解 (二)
View 的绘制系列文章: Android View 的绘制流程之 Measure 过程详解 (一) Android View 绘制流程之 DecorView 与 ViewRootImpl 在上一篇 ...
- 【朝花夕拾】Android自定义View篇之(六)Android事件分发机制(中)从源码分析事件分发逻辑及经常遇到的一些“诡异”现象
前言 转载请注明,转自[https://www.cnblogs.com/andy-songwei/p/11039252.html]谢谢! 在上一篇文章[[朝花夕拾]Android自定义View篇之(五 ...
- 【朝花夕拾】Android自定义View篇之(五)Android事件分发机制(上)Touch三个重要方法的处理逻辑
前言 转载请注明,转自[https://www.cnblogs.com/andy-songwei/p/10998855.html]谢谢! 在自定义View中,经常需要处理Android事件分发的问题, ...
- 【朝花夕拾】Android自定义View篇之(十一)View的滑动,弹性滑动与自定义PagerView
前言 由于手机屏幕尺寸有限,但是又经常需要在屏幕中显示大量的内容,这就使得必须有部分内容显示,部分内容隐藏.这就需要用一个Android中很重要的概念——滑动.滑动,顾名思义就是view从一个地方移动 ...
- java.lang.ClassCastException: android.view.ViewGroup$LayoutParams cannot be cast to android.widget.L(转)
09-09 10:19:59.979: E/AndroidRuntime(2767): FATAL EXCEPTION: main09-09 10:19:59.979: E/AndroidRuntim ...
- 解决 android.view.ViewGroup$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams
错误日志1: 06-13 10:55:50.410: E/KVLog(1129): Error info:java.lang.ClassCastException: android.widget.Li ...
- 接入新浪、腾讯微博和人人网的Android客户端实例 接入新浪、腾讯微博和人人网的Android客户端实例
做了个Android项目,需要接入新浪微博,实现时也顺带着研究了下腾讯微博和人人网的Android客户端接入,本文就跟大家分享下三者的Android客户端接入方法. 一.实例概述 说白了,接入微博就是 ...
- 整理最全的Android开发工程师面试题,面试题详解。java、Android程序员
1. 请描述下Activity的生命周期. 必调用的三个方法:onCreate()--> onStart() --> onResume(),用AAA表示 (1)父Activity启动 ...
- Android Studio精彩案例(三)《模仿微信ViewPage+Fragment实现方式二》
转载本专栏文章,请注明出处,尊重原创 .文章博客地址:道龙的博客 写在前面的话:此专栏是博主在工作之余所写,每一篇文章尽可能写的思路清晰一些,属于博主的"精华"部分,不同于以往专栏 ...
随机推荐
- php错误记录
1.模板不存在ThinkPHP\Library\Think\View.class.php LINE: 110 是因为IndexController的Index函数,而View中没有对应的Index文件 ...
- win7系统,apache2.2下添加PHP5的配置详解
首先要说apache(服务器). php(开发语言). mysql(数据库) 之间的关系. Apache:为系统提供了Web服务支持,网站:http://www.apache.org/ PHP:为系统 ...
- 规划(纪念我在ACM道路上的一年)
现在已经是晚上一点了,我早早的躺在床上,不能入睡,因为睡觉前看了一下我们学校今年区域赛的成绩总结,派出八次队伍,七个铜-- 再加上这两天ACM迎新杯的筹备过程的问题,让我产生了深深的思考-- 去年司老 ...
- hdu_5791_Two(DP)
题目链接:hdu_5791_Two 题意: 给你两串数列,问你相同的子序列有多少个,要注意,可以重复,比如1 和1 1 1 ,相同的子序列为3个 题解: 就和求最长公共子序列差不多,只不过要全部加起来 ...
- Linux 解决 bash ./ 没有那个文件或目录 的方法
在Debian 或 ubuntu 64位下运行 ./xxx 会跳出来说没有这个文件或者目录,但是ls看又有这个文件,很是奇怪. 其实原因很简单,是因为他没有32位的运行库 ia32-libs ,直接安 ...
- SharePoint 2013 APP 开发示例 (二)获取用户信息
SharePoint 2013 APP 开发示例 (二)获取用户信息 这个示例里,我们将演示如何获取用户信息: 1. 打开 Visual Studio 2012. 2. 创建一个新的 SharePo ...
- weka简介
1.weka的历史 1992年末,新西兰怀卡托大学计算机科学系Ian Written博士申请基金. 1993年获新西兰政府资助,并于同年开发出接口和基础架构. 1994年发布了第一个weka的内部版本 ...
- find the closest sum to a target value
problem: given an array of integers including positive and negative, a target value. find 2 numbers ...
- 给php代码添加规范的注释phpDocumentor
给php代码添加规范的注释更多参考 http://phpdoc.org/docs/latest/index.html在phpdocumentor中,注释分为文档性注释和非文档性注释.所谓文档性注释,是 ...
- springboot 打包
springboot 打包 先clean 然后 maven package 通过命令java -jar target/GoshenWepPro-0.1.0.jar运行程序