Android可绘制资源ID冲突?

Jar*_*ith 5 resources android zxing android-activity

我已经将 ZXing 1.7 的 /android 项目设置为我的主 Android 应用程序项目引用的库。作为快速测试/概念证明,我使用了 CaptureActivity,其方式与此处描述的方式相同: http: //damianflannery.wordpress.com/2011/06/13/integrate-zxing-barcod ...

大多数情况下,一切运行良好,我可以启动 CaptureActivity 并在启动它的活动中接收扫描的数据。然而,我得到了一些非常奇怪的行为,我认为这可能与我的项目和 /android ZXing 项目之间的资源 ID 冲突有关。这需要一些解释,所以请耐心等待......

我使用以下代码启动 CaptureActivity:

Button scanBtn = (Button)findViewById(R.id.mainmenu_btn_scan); 
scanBtn.setOnClickListener( 
    new View.OnClickListener() 
{ 
    public void onClick(View v) 
    { 
        Intent i = new Intent("com.google.zxing.client.android.SCAN"); 
        i.putExtra("SCAN_MODE", "QR_CODE_MODE"); 
        startActivityForResult(i, SCAN_CODE); 
    } 
}); 
Run Code Online (Sandbox Code Playgroud)

mainmenu_btn_scan在menu.xml中定义:

<LinearLayout android:id="@+id/mainmenu_view" style="@style/MainMenu" xmlns:android="http://schemas.android.com/apk/res/android">
    ...
    <Button android:id="@+id/mainmenu_btn_scan" 
                    android:drawableTop="@drawable/btn_scan_drawable" 
                    style="@style/MainMenuButton" 
                    android:text="Scan"/>
    ... 
</LinearLayout> 
Run Code Online (Sandbox Code Playgroud)

btn_scan_drawable.xml 位于 /drawable 中,是一个可绘制选择器:

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:drawable="@drawable/scan_btn_pressed" android:state_focused="true" android:state_pressed="true" /> 
    ...
</selector>
Run Code Online (Sandbox Code Playgroud)

奇怪的问题是这样的:当我以横向方向握住手机 并启动 CaptureActivity 时,我看到 scan_btn_pressed 图像在显示屏上拉伸,并且它的另一个副本也在“在取景器矩形文本内放置条形码”中拉伸。 .”。仅当我横向握住手机启动活动时才会发生这种情况。

我发现如果我更换

android:drawableTop="@drawable/btn_scan_drawable" 
Run Code Online (Sandbox Code Playgroud)

android:drawableTop="@drawable/scan_btn_pressed"
Run Code Online (Sandbox Code Playgroud)

问题消失了,一切正常,但我想让它与选择器一起工作(并理解问题发生的原因)。我的活动 UI 中的可绘制对象如何神秘地出现在另一个活动中?我是 Android 开发新手,但我整天都在谷歌搜索并处理这个问题,除了 android 文档中对资源 ID 冲突的一些模糊引用之外,我似乎无法找出为什么会发生这种情况。

注意:我仅使用 CaptureActivity 作为快速原型/概念验证。我将利用 ZXIng 库为最终应用程序实施我自己的活动。

Jar*_*ith 4

好吧,Android 平台在使用库项目时似乎有一些问题。我注意到我的项目的 R 类和 ZXIng 库项目的 R 类到处都是冲突的,例如

public static final class id {
    ...
    public static final int mainmenu_btn_scan=0x7f070028;
    ...
Run Code Online (Sandbox Code Playgroud)

在库项目的 R 类中:

public static final class id {
    ...
    public static final int share_app_button=0x7f070028;
    ...
Run Code Online (Sandbox Code Playgroud)

这解释了库项目中的活动如何从我的活动中选取 UI 元素。Android会优先考虑主项目中的资源id。

这篇优秀的文章给出了解决方案:http://blog.blackmoonit.com/2010/12/android-sharing-resources-in-eclipse.html

在 CaptureActivity (我在库中调用的活动)中,我替换了

viewfinderView = (ViewfinderView) findViewById(R.id.viewfinder_view);
Run Code Online (Sandbox Code Playgroud)

有了这个

viewfinderView = (ViewfinderView) findViewById(
    getResources().getIdentifier("viewfinder_view", "id", getPackageName()) );
Run Code Online (Sandbox Code Playgroud)

这解决了这个问题,尽管我需要检查并用 getIndentifier() 替换所有直接 id 引用,以确保不存在任何其他冲突。这不是一个理想的解决方案。感觉 aapt 工具应该有一些相当简单的扩展,以确保 R 类之间的 id 不会冲突。

请参阅此处了解更多信息:http://devmaze.wordpress.com/2011/05/22/android-application-android-libraries-and-jar-libraries/

  • 我不这么认为,真正的问题是 R 类中的 id 存在冲突。com.somelib.R.share_app_button == com.myapp.R.mainmenu_btn_scan,正在使用正确的 R 文件,但由于冲突和 Android 的资源优先级规则,库的 R 文件 ID 最终引用了我的应用程序中的资源。 (2认同)