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 );
}
}
注:文字大小也可以按比例缩放,不过手机与平板表现的不一样
本文详细介绍了如何使用Java代码遍历界面控件,并通过实例展示了如何实现全屏适应性调整大小的技术。主要内容包括使用instanceof进行控件类型判断、获取并操作子视图,以及如何在不同设备上实现自适应布局,确保应用在各种屏幕尺寸下都能良好显示。
612

被折叠的 条评论
为什么被折叠?



