viewGroup 项目中使用
生活随笔
收集整理的這篇文章主要介紹了
viewGroup 项目中使用
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
2019獨(dú)角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>
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){ //高度改變時(shí)通知容器重新改變高度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[] { "悟", "天機(jī)", "你好", "電話", "18601772389","發(fā)生的發(fā)大水飛" };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("請(qǐng)輸入用戶");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的實(shí)例if (imm.isActive()) {//如果開啟imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, InputMethodManager.HIDE_NOT_ALWAYS); //關(guān)閉軟鍵盤,開啟方法相同,這個(gè)方法是切換開啟與關(guān)閉狀態(tài)的}}return false;}});edit.setOnKeyListener(new OnKeyListener() {@Overridepublic boolean onKey(View v, int keyCode, KeyEvent event) {if (keyCode == KeyEvent.KEYCODE_ENTER) { // 監(jiān)聽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點(diǎn)擊事件* * @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); // 刪除狀態(tài)圖片}} else {// 設(shè)置按鈕的狀態(tài)為可以刪除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); // 正常顯示狀態(tài)圖片}}}}}<?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>
轉(zhuǎn)載于:https://my.oschina.net/xiahuawuyu/blog/104066
總結(jié)
以上是生活随笔為你收集整理的viewGroup 项目中使用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 大唐电信JAVA笔试题面试题
- 下一篇: CentOS系统搭建OpenERP