日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

viewGroup 项目中使用

發布時間:2025/4/16 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 viewGroup 项目中使用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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 项目中使用的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。