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 等)。