android子view获取父布局,Android获取布局父ID(Android get layout parent id)
Android獲取布局父ID(Android get layout parent id)
我想知道View和ViewParent有什么區別? 我想獲取ImageView父級的Id,但我不能這樣做:
myImageView.getParent().getId();
那么有另一種獲得此ID的方法嗎?
I would like to know what is the difference between View and ViewParent ? I am trying to get the Id of the parent of an ImageView but this I can't do :
myImageView.getParent().getId();
So is there another way to get this id ?
原文:https://stackoverflow.com/questions/22759352
更新時間:2020-01-07 18:13
最滿意答案
我想知道View和ViewParent有什么區別?
View是一個類, ViewParent是一個接口。
雖然許多常見的布局類實現了ViewParent接口,但不能保證。
您遇到的問題是myImageView.getParent()返回的ViewParent不直接公開getId()方法。
正如其他人所說,使用...將ViewParent轉換為View
((View) myImageView.getParent()).getId();
......應該在編譯時工作,但要注意以下事項......
如果父View未實現ViewParent接口,則轉換將失敗。
父View必須在布局文件中定義資源ID為(例如) android:id=@+id/myParentViewId或對getId的調用將返回null
I would like to know what is the difference between View and ViewParent ?
A View is a class and a ViewParent is an interface.
Although many of the common layout classes implement the ViewParent interface it isn't guaranteed.
The problem you're having is that the myImageView.getParent() is returning a ViewParent which doesn't directly expose a getId() method.
As others have said, casting the ViewParent to a View using...
((View) myImageView.getParent()).getId();
...should work at compile time but be aware of the following...
If the parent View doesn't implement the ViewParent interface then the cast will fail.
The parent View must have a resource id defined in the layout file as (for example) android:id=@+id/myParentViewId or the call to getId will return null
2014-03-31
相關問答
您是否已使用以下代碼將其附加到活動中? setContentView(R.layout.layout_id);
have you already attached it to the activity by using following code? setContentView(R.layout.layout_id);
你是對的, DecorView將使用FrameLayout來存儲應用程序設置的所有內容。 您可以閱讀PhoneWindow的來源 You're right, DecorView will use a FrameLayout to store all the content set by applications. You can read the source of PhoneWindow
在我看來 - 沒有ID,你無法訪問views.U可以去tag.for在視圖中你可以setTag(),你可以getTag()。我們也可以使用標簽作為ID somecases。 int ViewGroup.indexOfChild(View child) will help me. I will write more, when I test all decigion.
如果ViewGroup.LayoutParams不適用于你,那么你需要檢查一個instanceof ,看看布局參數是否屬于你可以使用的類型: ViewGroup.LayoutParams layoutParams = aktUIItem.getLayoutParams();
if (layoutParams instanceof FrameLayout.LayoutParams) {
FrameLayout.LayoutParams frameLayoutParams = (FrameLay
...
它可以是相同的。 但是為了避免混淆/模糊,最好使用blackbelt建議的不同ID。 您可以找到設置為活動的當前視圖層次結構的findViewById 。 因此,如果您在不同的xml布局中具有相同的ID,那就沒問題。 如果你有以下 setContentView(R.layout.main)
Button b = (Button) findViewById(R.id.btnClose); // initialize button
您可以找到當前視圖層次結構的ViewById。 在你的情況下main
...
好的我沒有找到它為什么這樣做,但在運行Eclipse之前在設備上手動卸載應用程序清除了錯誤... Ok I did not find out why it was doing this but uninstalling the app manually on devices before runing through Eclipse cleaned the error...
我想知道View和ViewParent有什么區別? View是一個類, ViewParent是一個接口。 雖然許多常見的布局類實現了ViewParent接口,但不能保證。 您遇到的問題是myImageView.getParent()返回的ViewParent不直接公開getId()方法。 正如其他人所說,使用...將ViewParent轉換為View ((View) myImageView.getParent()).getId();
......應該在編譯時工作,但要注意以下事項...... 如
...
除了@Barak之外,您在TabHost之后缺少線性布局 <?xml version="1.0" encoding="utf-8"?>
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
...
我曾經得到這個錯誤,但現在我使用@BindingAdapter修復它。 @BindingAdapter(value = "app:headerLayout", requireAll = false)
public static void inflateHeaderLayout(NavigationView navigationView, @LayoutRes int layoutRes) {
navigationView.inflateHeaderView(layoutRes);
}
其
...
總結
以上是生活随笔為你收集整理的android子view获取父布局,Android获取布局父ID(Android get layout parent id)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android适配器以及作用,Andro
- 下一篇: android bitmap 占用内存大