簡體   English   中英

從XML文件中的支持庫引用資源ID

[英]Referencing resource ID from the support library in XML file

導入使用在v7 supoort庫中創建的資源ID的xml文件。

導入的xml文件包含以下ID,但不使用“ @ id +”,就好像它是剛剛創建的一樣:

android:id =“ @ id / activity_chooser_view_content”

在以下鏈接中,它表明在v7 appCompat支持庫中已經創建了ID(在注釋末尾部分的第3行向下):

http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.3_r1/frameworks/support/v7/appcompat/res/layout/abc_activity_chooser_view.xml

 android:id="@+id/activity_chooser_view_content"

為了讓Android或Android Studio查找ID,我該怎么辦?

我收到以下錯誤:

Error:(2, 51) No resource found that matches the given name (at 'id' with value '@id/activity_chooser_view_content').

Error:(8, 64) No resource found that matches the given name (at 'id' with value '@id/image').

我正在編譯以下參考庫:

compile 'com.android.support:support-annotations:23.1.0'
compile 'com.android.support:appcompat-v7:25.1.0'
compile 'com.android.support:design:25.1.0'


compile 'com.android.support:multidex:1.0.1'
compile 'com.android.support:support-v13:25.0.0'

為了從SDK引用ID,您需要@android:id/<name>

否則,您將出錯,這意味着由於您擁有@id而不是@+id ,因此您試圖從自己的應用程序資源中引用一個現有ID,而不是創建一個新ID。

或者也許你應該嘗試類似的東西

View rootView = LayoutInflater.from(getContext()).inflate(android.R.layout.abc_activity_chooser_view);
View x = rootView.findViewById(android.R.id.<name>);

值得一提 ...

內部布局不打算直接使用,並且隨時可能更改,這可能會在將來破壞您的代碼。 更好的解決方案是創建自己的布局或使用具有類似功能的公共框架小部件。 內部小部件的使用由您自擔風險

https://stackoverflow.com/a/39318656/2308683

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM