导航:首页 > 操作系统 > android测量控件

android测量控件

发布时间:2022-05-08 00:33:37

A. android 怎么在代码中获取控件的属性值

如果是自定义的控件可以用一下代码TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.MyToggleBtn);// 由attrs 获得 TypeArray,
如果是系统自带的控件,通常控件点get会有对应的获取属性值的方法,如textView.getHeight,不过有些方法如margin,就要通过layoutparam去获取设置

B. 如何学会使用安卓自动化测试工具MonkeyRunner

第一步、安卓自有目录\tools\hierarchyviewer.bat工具可以用来查看应用程序的ID。

(1)、启动安卓模拟器,打开需要查看ID的软件界面。此处以google搜索界面为例。然后双击打开hierarchyviewer.bat,显示搜索界面的activity,即下图左侧被选中的项,表示搜索界面的完整包名。

(2)点击Load View Hierarchy按钮,展现该搜索界面的层级图。选中指定的控件,查看ID名称。

在属性显示区域可以看到各个控件所处的坐标位置,以及可以查看模拟器上任务栏的高度,这些信息可以用于坐标计算中。

第二步、通过EasyMonkeyDevice类和By类来调用控件ID。

(1)输入框的ID写法:easy_device.type(By.id('id/name_text),'zhangsan')。

(2)复选框/单选/按钮的ID写法:easy_device.touch(By.id('id/login_button'),MonkeyDevice.DOWN_AND_UP)。

(3)当两个ID名称相同时,可以使用层级进行定位。

easy_device.touch(By.id('id/parent_button'),MonkeyDevice.DOWN_AND_UP,By.id('id/current_button'),MonkeyDevice.DOWN_AND_UP)。

第三步、运行文件模拟计算7*8=56,生成result.png。monkeyrunner calculator.py。

用ID进行参数差不多都是这个思路。但是对于列表、或者弹出框则无法直接通过点击ID操作成功,需要计算ID的坐标。

软件测试工程师经过以上三个步骤,我们就能快速的掌握MonkeyRunner的使用方法,完成安卓软件测试的任务。

C. 如何正确获取Android控件的高度

Android动态改变View控件大小的方法: 1、声明控件参数获取对象 LayoutParams lp; 2、获取控件参数: lp = 控件id.getLayoutParams(); 3、设置控件参数:如高度。 lp.height -= 10; 4:、使设置生效:控件id.setLayoutParams(lp); 例如如要把Imageview下移200px: ImageView.setPadding( ImageView.getPaddingLeft(), ImageView.getPaddingTop()+200, ImageView.getPaddingRight(), ImageView.getPaddingBottom());

D. 如何获取Android界面的控件属性

objective c 怎样获取界面控件
iewport全部属性&值如下:width: viewport宽度
height: viewport高度
initial-scale: 初始缩放比例
maximum-scale: 最大缩放比例
minimum-scale: 最小缩放比例
user-scalable: 是否允许用户缩放例:width=960 或 device-width
height=1000 或 device-height
initial-scale=0.5
maximum-scale=2
minimum-scale=1
user-scalable=1 或 0 (yes 或 no)layout viewport的默认值在Apple实现viewport后,其他浏览器也加入了对viewport meta的支持,但彼此间还是有些差异,差异最大的是layout viewport的表现:Safari iPhone: 980px
Opera: 850px
Android WebKit: 800px

E. android 在onCreate中获得控件的大小

这个方法并不是适合所有场景,这个方法获取的宽度是minWidth参数设置的大小和background指定背景宽度,这两个宽度的最大值,高也是如此,也就是说如果View的xml中没有两个参数中的其中一项,那么这个方法测量的宽高也是为0的,这个方法测量的并不是获取xml中设置的android:layout_height android:layout_width的值,为什么这么说了,看源码
imageView.measure(w, h); -->调用View的measure方法-->onMeasure()方法,onMeasure源码:

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(),widthMeasureSpec),

getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));

}
onMeasure->setMeasuredDimension()->getDefaultSize()>getSuggestedMinimumHeight()
这个是源码onMeasure中方法调用过程,逆向分析方法源码:
getSuggestedMinimumHeight():
protected int getSuggestedMinimumHeight() {

return (mBackground == null) ? mMinHeight : max(mMinHeight,mBackground.getMinimumHeight());

}
如果背景为空,那么就取mMinHeight的值,如果背景不为空就取max(mMinHeight,mBackground.getMinimumHeight())背景高度和mMinHeight最大值
接下来获取建议值完毕后查看getDefaultSize的源码:

//第一个参数是getSuggestedMinimumHeight方法获取的建议值 第二个参数是系统计算得出的宽高规格是MeasureSpec值,也就是measure(w,h)中的w或者h,
public static int getDefaultSize(int size, int measureSpec) {

int result = size;
//int w = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);

//规格模式不就是上面的:View.MeasureSpec.UNSPECIFIED
int specMode = MeasureSpec.getMode(measureSpec);
//规格模式不就是上面的 0
int specSize = MeasureSpec.getSize(measureSpec);

switch (specMode) {//这里是什么了?View.MeasureSpec.UNSPECIFIED理解吧

case MeasureSpec.UNSPECIFIED://
//result就是getDefaultSize要返回的值,根据switch判读getDefaultSize返回的是什么了
//不就是方法的第一个形参吗,这个形参不就是宽高建议值吗
//也就是max(mMinHeight,mBackground.getMinimumHeight());
result = size;
break;
case MeasureSpec.AT_MOST:
case MeasureSpec.EXACTLY:

result = specSize;

break;

}
return result;
}

好了,现在就是setMeasuredDimension方法了,源码:
protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {

boolean optical = isLayoutModeOptical(this);

if (optical != isLayoutModeOptical(mParent)) {

Insets insets = getOpticalInsets();

int opticalWidth = insets.left + insets.right;

int opticalHeight = insets.top + insets.bottom;

measuredWidth += optical ? opticalWidth : -opticalWidth;

measuredHeight += optical ? opticalHeight : -opticalHeight;

}

mMeasuredWidth = measuredWidth;//这个是这个方法要注意的值

mMeasuredHeight = measuredHeight;//同上

mPrivateFlags |= PFLAG_MEASURED_DIMENSION_SET;

}
这个代码好长啊,好多东西,要关注的就是注释的代码,上面要注意的两行代码有什么用了
你再看一个方法的源码你就是知道了,getMeasureWidth()与getMeasureHeight():
public final int getMeasuredWidth() {

return mMeasuredWidth & MEASURED_SIZE_MASK;

}
public final int getMeasuredHeight() {

return mMeasuredHeight & MEASURED_SIZE_MASK;

}
这两个方法不就是返回调用measure测量的宽高吗?不就是上面两行注意的代码的值吗
现在回答你的问题:
这是代码,我想问makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED)中的一个参数为什么是0,什么意思?

第一个参数本应该是系统测量该View后得到的规格值(MeasureSpec),本来这个measure是由系统测量完宽高后自动调用,我们这里只是做了系统即将要做的事情而已,那么这个参数为什么是0了,既然我们要通过这个方法测量View的宽高,不就是怕系统还没有自动调用这个方法前调用getMeasureWidth/Height方法而没法获得导致取值为0 ,也就是我们默认调用这个方法就是系统没有对该View绘制,就直接调用了measure方法,所以也就是宽高为0咯,其实这
makeMeasureSpec的第一个参数设置什么都无所谓啦,因为最后取得值也不是第一个参数设置的值,我觉得我的表达好绕啊,不过要是你对measure的绘制机制的源码很熟悉的话,应该是没问题的,这里我推荐你去看(谷歌的小弟)csdn的博客里面有完整的源码分析,你要以前看的不是很懂,去看看他写的博客应该会有点启发

F. android 怎么在布局里面获取控件

layout为布局,布局里面可以放任何空间,获取空间可以用findViewById方法获取

android 获取某个布局控件 添加到另一个布局中

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

LinearLayout relativeLayout = (LinearLayout) findViewById(R.id.layout456);
ImageView imgApple2 = new ImageView(this);
imgApple2.setImageResource(R.drawable.ic_launcher);
relativeLayout.addView(imgApple2);

LayoutInflater factorys = LayoutInflater.from(MainActivity.this);
final View textEntryView = factorys.inflate(R.layout.layout1, null);

// LinearLayout linearLayout = (LinearLayout) textEntryView
// .findViewById(R.id.layout1);
// relativeLayout.addView(linearLayout);
EditText editText1 = (EditText) textEntryView
.findViewById(R.id.editText1);

relativeLayout.addView(editText1);

G. android如何获取控件宽度

用getWidth()方法,可以获取像素单位的宽度。

android的控件一般是继承的android.View这个类,所以可以直接用View#getWidth()方法获取控件宽度。另外这个方法是final方法,无法被子类覆盖,所以可以安心调用

H. Android获取控件高度怎样才能不为0

一句话,onCreate onStart onResume的时候,所有的View还没有被添加到视图树上,所以无法得到
所以,需要重写onWindowFocusChanged方法,这个是所有的View被添加到视图上时会执行的方法

@Override<strong>public void </strong>onWindowFocusChanged(<strong>boolean </strong>hasFocus) { <strong>super</strong>.onWindowFocusChanged(hasFocus); <strong>int </strong>heigth = <strong>root</strong>.getHeight();}

即可。

I. Android如何判断控件获取焦点啊

解决了!添加OnTouchListener侦听,在MotionEvent.ACTION_UP的时候弹出对话框

J. android怎么获取 中的控件

在实际开发中LayoutInflater这个类还是非常有用的,它的作用类似于findViewById()。不同点是LayoutInflater是用来找res/layout/下的xml布局文件代码块,并且实例化;而findViewById()是找xml布局文件下的具体widget控件(如Button、TextView等)。 具体作用:
1、对于一个没有被载入或者想要动态载入的界面,都需要使用LayoutInflater.inflate()来载入;
2、对于一个已经载入的界面,就可以使用Activiyt.findViewById()方法来获得其中的界面元素。
LayoutInflater 是一个抽象类,在文档中如下声明:
public abstract class LayoutInflater extends Object
获得 LayoutInflater 实例的三种方式:
1.LayoutInflater inflater = getLayoutInflater(); //调用Activity的getLayoutInflater()
2.LayoutInflater localinflater =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
1. LayoutInflater inflater = LayoutInflater.from(context);
其实,这三种方式本质是相同的,从源码中可以看出:
getLayoutInflater():
Activity 的 getLayoutInflater() 方法是调用 PhoneWindow 的getLayoutInflater()方法,看一下该源代码:
public PhoneWindow(Context context) {
super(context);
mLayoutInflater = LayoutInflater.from(context);
}
可以看出它其实是调用 LayoutInflater.from(context)。
LayoutInflater.from(context):
public static LayoutInflater from(Context context) {
LayoutInflater LayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (LayoutInflater ==null) {
throw new AssertionError("LayoutInflater not found.");
}
return LayoutInflater;
}
可以看出它其实调用 context.getSystemService()。
结论:所以这三种方式最终本质是都是调用的Context.getSystemService()。
inflate 方法 通过 sdk 的 api 文档,可以知道该方法有以下几种过载形式,返回值均是 View 对象,如下:
public View inflate (int resource, ViewGroup root);
3 public View inflate (XmlPullParser parser, ViewGroup root);
4 public View inflate (XmlPullParser parser, ViewGroup root, boolean attachToRoot);
5 public View inflate (int resource, ViewGroup root, boolean attachToRoot);
6
7 LayoutInflater inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
8 View view = inflater.inflate(R.layout.custom, (ViewGroup)findViewById(R.id.test));
9 //EditText editText = (EditText)findViewById(R.id.content);
10 // error
EditText editText = (EditText)view.findViewById(R.id.content);
对于上面代码,指定了第二个参数 ViewGroup root,当然你也可以设置为 null 值。
注意:
·inflate方法与 findViewById 方法不同;
·inflater 是用来找 res/layout下的 xml 布局文件,并且实例化;
·findViewById() 是找具体 xml 布局文件中的具体 widget 控件(如:Button、TextView 等)。

阅读全文

与android测量控件相关的资料

热点内容
成都python培训机构好不好 浏览:421
mysql查看配置命令 浏览:597
v8编译cmake 浏览:965
app品牌起步阶段需要什么营销 浏览:358
压缩机制冷剂温度 浏览:930
会日语的程序员 浏览:19
网银密码加密失败怎么回事 浏览:727
android开发音乐播放器 浏览:808
ug120阵列命令快捷键 浏览:597
气动隔膜式压缩机 浏览:470
linux如何修改主机名 浏览:104
单片机光标上下移动 浏览:528
数据加密验证 浏览:108
程序员被激怒 浏览:891
winxp找不到服务器dns地址 浏览:842
以文本文件的格式保存考生文件夹 浏览:41
编译原理文法分为几类 浏览:570
JAVA基础学python要多久 浏览:74
java流量控制 浏览:936
java实现多重继承 浏览:707