当前位置: 代码迷 >> Android >> Android界面控件遍历朝历代码 改变宽度 高度 字体大小适应屏幕
  详细解决方案

Android界面控件遍历朝历代码 改变宽度 高度 字体大小适应屏幕

热度:469   发布时间:2016-05-01 19:13:23.0
Android界面控件遍历代码 改变宽度 高度 字体大小适应屏幕

1.遍历界面控件

?

用instanceof来判断是否是指定的控件类型

LinearLayout myLayOut = (LinearLayout)findViewById(R.id.tableLayout1);

LinearLayout 是父控件名称,根据你自己的修改

?

可以用这句得到

LinearLayout loginLayout = (LinearLayout) getLayoutInflater().inflate(R.layout.main, null);

?

for (int i = 0; i < myLayOut.getChildCount(); i++) {View v = myLayOut.getChildAt(i);if ( v instanceof ImageView){ImageView myImageView = (ImageView)myLayOut.getChildAt(i);myImageView.setOnClickListener(new myOnclickListener());}}

注:只能得到一级子View

?

2.适应全屏修改大小

?

changeLayoutParams(R.id.tableLayout1);

?

protected void changeLayoutParams(int layoutId){  final View view = (View) findViewById(layoutId);  ViewTreeObserver viewTreeObserver = view.getViewTreeObserver();  viewTreeObserver.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {    boolean isFirst = true;            @Override            public void onGlobalLayout() {      if (isFirst) {               isFirst = false; //执行一次 不这样设置 他会一直执行                         int height = view.getMeasuredHeight();                         int width = view.getMeasuredWidth();                         //viewGroup 适应所有父布局控件                         ViewGroup.LayoutParams linearParams = (ViewGroup.LayoutParams) view.getLayoutParams();    		         linearParams.width = (int) (width*scale);    		         linearParams.height = (int) (height*scale); //在原有的高度 宽度放大倍数    		        view.setLayoutParams(linearParams);            	 }            }		});		if(view instanceof TextView){			((TextView) view).setTextSize( 24  );		}else if(view instanceof EditText){			((EditText) view).setTextSize(  24);		}else if(view instanceof Button){			((Button) view).setTextSize( 24 );		}else if(view instanceof RadioButton){			((RadioButton) view).setTextSize( 24 );		}	}

注:文字大小也可以按比例缩放,不过手机与平板表现的不一样

?

1 楼 zwj2009 2012-04-06  
请问changeLayoutParams方法你是在什么时间调用呢

我在onCreate方法中调用无效
  相关解决方案