1
最近,我正在研究一個需要該視圖的絕對路徑的android項目。如何在android中獲取視圖的絕對路徑?
例如,在的LinearLayout一個button_0
button_0的絕對路徑是
「DecorView>的LinearLayout [0]>的FrameLayout [1]> RelativeLayout的[0]>按鈕[0]」:」 button_0「
我怎麼能自動獲得這個絕對路徑?
最近,我正在研究一個需要該視圖的絕對路徑的android項目。如何在android中獲取視圖的絕對路徑?
例如,在的LinearLayout一個button_0
button_0的絕對路徑是
「DecorView>的LinearLayout [0]>的FrameLayout [1]> RelativeLayout的[0]>按鈕[0]」:」 button_0「
我怎麼能自動獲得這個絕對路徑?
你可以保持在視圖中像這樣的東西往上走:
Button startingButton = findViewById(R.id.startingButton);
ViewParent v = startingButton.getParent();
while(v != null) {
System.out.println(String.valueOf(v.getId()) + " : " + v.getClass().getName());
v = v.getParent();
}
這會讓你所有的ID和類名(「android.widget.ImageView」,例如)。
非常感謝。但'View v = startingButton.getParent();'應該是'ViewParent v = startingButton.getParent();' – stackvoid
@ cellodrunk固定的。謝謝。 –