2019独角兽企业重金招聘Python工程师标准>>>

package com.cn.mc;import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;/*** @author yutao**/public class MyViewGroup extends ViewGroup {private final static int VIEW_MARGIN=2;  //间距2像素private int myHeight=0;private Handler handler=null;public MyViewGroup(Context context) {super(context);}public MyViewGroup(Context context,Handler handler) {super(context);this.handler=handler;}public MyViewGroup(Context context,AttributeSet set){super(context, set);setScrollContainer(true);}@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {for (int index = 0; index < getChildCount(); index++) {final View child = getChildAt(index);child.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);}super.onMeasure(widthMeasureSpec, heightMeasureSpec);}@Overrideprotected void onLayout(boolean arg0, int arg1, int arg2, int arg3, int arg4) {final int count = getChildCount();int row=0;             // which row lay you view relative to parentint lengthX=arg1;    // right position of child relative to parentint lengthY=arg2;    // bottom position of child relative to parentfor(int i=0;i<count;i++){final View child = this.getChildAt(i);int width = child.getMeasuredWidth();int height = child.getMeasuredHeight();lengthX+=width+VIEW_MARGIN;lengthY=row*(height+VIEW_MARGIN)+VIEW_MARGIN+height+arg2;if(lengthX>arg3){lengthX=width+VIEW_MARGIN+arg1;row++;lengthY=row*(height+VIEW_MARGIN)+VIEW_MARGIN+height+arg2;}child.layout(lengthX-width, lengthY-height, lengthX, lengthY);}if(myHeight!=lengthY){   //高度改变时通知容器重新改变高度myHeight=lengthY;Message msg=new Message();msg.what=myHeight;handler.sendMessage(msg);// System.out.println("高度:"+lengthY);}}public int getMyHeight() {return myHeight;}public void setMyHeight(int myHeight) {this.myHeight = myHeight;}}
package com.cn.mc;import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;import android.support.v4.app.NotificationCompat.Action;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.ScrollView;public class TestActivity extends Activity {private EditText edit;List<View> listView = new ArrayList<View>();String[] texts = new String[] {  "悟", "天机", "你好", "电话", "18601772389","发生的发大水飞" };private List<String> buttonText = new ArrayList<String>(Arrays.asList(texts));private MyViewGroup viewGroup;private ScrollView scrollView;private RelativeLayout container;private int svMaxHeight = 200;private Handler handler = new Handler() {public void handleMessage(android.os.Message msg) {int height = msg.what;container.removeAllViews();if (height > 74 && height <= svMaxHeight) {LayoutParams params = (LayoutParams) scrollView.getLayoutParams();params.height = svMaxHeight;scrollView.setLayoutParams(params);System.out.println("改变scrollView大小");} else if (height == 74) {LayoutParams params = (LayoutParams) scrollView.getLayoutParams();params.height = 72;scrollView.setLayoutParams(params);}params.height = height;System.out.println("height:" + height);container.addView(viewGroup, params);};};private LayoutParams params = null;private int height = 0;protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.test);scrollView = (ScrollView) findViewById(R.id.scrollView1);container = (RelativeLayout) findViewById(R.id.container);height = LayoutParams.WRAP_CONTENT;params = new LayoutParams(LayoutParams.FILL_PARENT, height);viewGroup = new MyViewGroup(this, handler);viewGroup.setBackgroundColor(getResources().getColor(R.color.white));/** for(String s:buttonText){ Button b=new Button(this);* b.setBackgroundResource(R.drawable.menu_bg_pressed); b.setHeight(72);* b.setText(s); b.setOnClickListener(new MyButtonListener());* viewGroup.addView(b); }*/edit = new EditText(this);edit.setHeight(72);edit.setHint("请输入用户");edit.setWidth(350);edit.setBackgroundColor(Color.TRANSPARENT);viewGroup.addView(edit, viewGroup.getChildCount());edit.setOnTouchListener(new OnTouchListener() {@Overridepublic boolean onTouch(View v, MotionEvent event) {if(event.getAction() == MotionEvent.ACTION_DOWN){InputMethodManager imm = (InputMethodManager)getSystemService(TestActivity.INPUT_METHOD_SERVICE);  //得到InputMethodManager的实例if (imm.isActive()) {//如果开启imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, InputMethodManager.HIDE_NOT_ALWAYS); //关闭软键盘,开启方法相同,这个方法是切换开启与关闭状态的}}return false;}});edit.setOnKeyListener(new OnKeyListener() {@Overridepublic boolean onKey(View v, int keyCode, KeyEvent event) {if (keyCode == KeyEvent.KEYCODE_ENTER) { // 监听enter键String s = edit.getText().toString().trim();if (!s.equals("")) {Button b = new Button(TestActivity.this);b.setBackgroundResource(R.drawable.menu_bg_pressed);b.setHeight(72);b.setText(s);b.setOnClickListener(new MyButtonListener());viewGroup.addView(b, viewGroup.getChildCount() - 1);edit.setText(null);}}if (event.getAction() == KeyEvent.ACTION_DOWN&& keyCode == KeyEvent.KEYCODE_DEL) {Button btn = (Button) viewGroup.getChildAt(viewGroup.getChildCount() - 2);if(btn == null){return false;}else{clickFunction(btn);}}return false;}});container.removeAllViews();params.height = viewGroup.getMyHeight();container.addView(viewGroup, params);}/*** button点击事件* * @author yutao* */class MyButtonListener implements OnClickListener {@Overridepublic void onClick(View v) {Button b = (Button) v;clickFunction(b);}}public void clickFunction(Button btn) {Object o = btn.getTag();System.out.println("o:" + o);if (o != null) {boolean tag = (Boolean) o;if (tag) {viewGroup.removeView(btn);} else {btn.setTag(true);btn.setBackgroundResource(R.drawable.dial_input_text_bg_normal); // 删除状态图片}} else {// 设置按钮的状态为可以删除btn.setTag(true);btn.setBackgroundResource(R.drawable.dial_input_text_bg_normal);}//  btn.setFocusable(false);int length = viewGroup.getChildCount();for (int i = 0; i < length; i++) {View child = viewGroup.getChildAt(i);if (child instanceof Button) {if (!child.equals(btn)) {child.setTag(false);child.setBackgroundResource(R.drawable.menu_bg_pressed); // 正常显示状态图片}}}}}


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent"android:background="#ffffff"android:id="@+id/main"><LinearLayout android:layout_width="fill_parent"android:layout_height="wrap_content"><ScrollView android:id="@+id/scrollView1"android:layout_width="fill_parent"android:layout_height="72px"android:background="#ff0000"android:scrollbarAlwaysDrawVerticalTrack="true"android:layout_weight="1"android:layout_marginRight="20dip"><RelativeLayout android:id="@+id/container"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="#00ff00"></RelativeLayout></ScrollView><ImageView android:id="@+id/sms_addcontact_img"android:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@drawable/ic_calllog_addcontact_normal"android:layout_weight="0"android:layout_marginTop="10dip"android:layout_marginRight="10dip"/></LinearLayout></RelativeLayout>


转载于:https://my.oschina.net/xiahuawuyu/blog/104066

viewGroup 项目中使用相关推荐

  1. 在原有Android项目中快速集成React Native

    前言 对于现有的大多数项目来说都不是从头构建的,而要在原有项目的基础上引入React Native则肯定和用react-native init xxx创建工程不同.因此下面就来说下具体操作.不过在真正 ...

  2. 项目中的一个技术方案替换历程(surfaceview+fragment 变成悬浮窗window)

    背景: 项目中UI层有SurfaceView,其渲染展示的是摄像机等采集画面,但是测试提了一个问题单,如果在当前页面中跳出到其他页面,会crash,经过log分析,是由于surfaceview 在失去 ...

  3. android 热门开源库,GitHub安卓热门开源资源在项目中的使用及项目总结

    前面一篇文章 <android studio的入门使用> 已经讲了如何导入一个开源的项目.本文则来讲如何使用这些开源的资源,以及在一个小app的开发中遇到的问题.因为不可能面面俱到,所以争 ...

  4. Android Studio中如何将ijkplayer 0.6.3导入自己的项目中并使用

    Ijkplayer 0.6.3是较新的版本,其中的ijk-example增强了不少功能,但是里面的一些资源由于增加了依赖,使得要使用ijk-example中的资源,不像以前版本那样只须把我们需要的几个 ...

  5. 关于Android项目中的Toast那些动画实现方式

    最近产品给了一个竞品App的Toast动画,希望开发可以去实现它,经过一段时间的深(不)思(停)熟(百)虑(度)之后,发现事情其实并不简单,所以这里记录一下关于Android~Toast动画实现的相关 ...

  6. standupTimer项目中的布局容器

    standupTimer是一个开源项目,这个项目涉及的Android知识点并不是太多也不复杂,所以对于初学者来说,读起来并不困难. 关于这个项目有个系列博客,系统地介绍了standupTimer是项目 ...

  7. Java项目中使用频率最高的100个类

    Java软件开发的很大一部分都使用来自各种库的API.我从10,000个开源Java项目中提取了API类的频率.这些类来自Java标准库或第三方库.每个类别对每个项目计一次.以下列表显示了前100名. ...

  8. android 美团商家详情页,Android仿美团团购详情页下拉图片放大效果,简单可直接用在项目中...

    一:介绍 大家在项目中,可能需要像美团团购详情页面下拉的时候美食图片放大的效果,在这里就给大家介绍如何实现这种效果,只有很少的代码,而且控件全部是安卓源生控件. 二:运行效果图 三.然后来看看如何实现 ...

  9. 安卓淘宝商店界面之高仿类CollapsinToolBar 直接搬运到项目中

      先说明一下这是我下载下来的demo稍作修改的,并不是原创,找不到原文链接了,如各位朋友有发现,请回复一下链接,谢谢!    OK 先上GIF看看效果    首先看看主界面: MainActivit ...

最新文章

  1. js判断数据类型是哪种
  2. Linux用户查看系统有多少用户在登录
  3. 机器学习之贝叶斯分类(python实现)
  4. 上海启动5G试用!104页PPT,为你深度解析5G终端的创新和机遇
  5. Canvas2~茜色的调色盘可奈线12月2日剧情翻译
  6. 【大局观很关键】关于找程序的bug
  7. 小型项目服务器要多少,小型服务器需要什么配置
  8. python数据库在哪_python可以用哪些数据库
  9. 全志线刷工具如何刷linux,全志 Allwinner V3S 开发环境搭建 (二)安装必要工具
  10. 【Leetocde | 24 】152. 乘积最大子序列
  11. ipad中的active失效?
  12. c语言课后题答案83,C语言练习试题和答案
  13. Firebug工具的介绍与安装
  14. 卡特彼勒CAT SIS 售后服务系统3D零件图系统软件 2019年最新版
  15. vlookup匹配的文字显示0_Excel | VLOOKUP函数使用详解
  16. 体育专业国培计算机感言,信息技术国培感言
  17. 纯css写一颗跳动的心
  18. Matlab GUI编程技巧(十二):menu创建菜单或菜单项
  19. java计算机毕业设计桂林恒保健康防护有限公司官网MyBatis+系统+LW文档+源码+调试部署
  20. 奶块服务器维护多久,奶块服务器维护是什么意思quest; | 手游网游页游攻略大全...

热门文章

  1. 项目启动时socket自动启动_spring boot 项目在启动时调用接口
  2. java contions_Java基础---数组总结
  3. 使用dokcer搭建个人博客网站
  4. Spark详解(十二):Spark Streaming原理和实现
  5. Zookeeper分布式一致性原理(六):Zookeeper开源客户端zkClient
  6. python selenium爬虫豆瓣_Python爬虫:学习selenium的正确方式
  7. 阐述计算机历程以及未来发展方向,计算机程序的发展史
  8. windows系统下安装JDK8的教程图解
  9. php5.3中ZendGuardLoader与wincache冲突问题的解决方法
  10. 【转】tf中的padding方式SAME和VALID有什么区别?