日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

android子view获取父布局,Android获取布局父ID(Android get layout parent id)

發布時間:2025/3/21 Android 50 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。