即时运行不适用于layout-21

时间:2016-05-06 16:33:21

标签: android android-studio android-gradle android-studio-2.0 android-instant-run

以下是在AS中即时运行不起作用的情况:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    toolbar.setElevation(visible ? getResources().getDimension(R.dimen.elevation_toolbar) : 0);
} else {
    View toolbarShadow = findViewById(R.id.toolbar_shadow);
    toolbarShadow.setVisibility(visible ? View.VISIBLE : View.GONE);
}

layout中的视图包含R.id.toolbar_shadow。一切都好。

但是,如果您在layout-21中有一个不包含R.id.toolbar_shadow的视图,那么当您将应用编译为例如Api 23设备gradle将失败并显示:

Error:(1046, 42) error: cannot find symbol variable toolbar_shadow

有什么想法可以解决这个问题吗?

更新:根据要求提供布局:

res/layout/actionbar.xml

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    ... >

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        ... />

    <View
        android:id="@+id/toolbar_shadow"
        ... />

</LinearLayout>

res/layout-21/actionbar.xml

<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toolbar"
            ... />

1 个答案:

答案 0 :(得分:0)

好的,发现解决方案手动添加了资源中缺少的ID:

<!--To make Instant Run work if these ids are not in 21+-->
<item name="toolbar_shadow" type="id"/>
相关问题