首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 更多程式語言 >> 程式設計綜合問答

Android Studio Layout Render 問題:Failed to find style 對應 id,切換 API Level 解決

     編輯:程式設計綜合問答

現象

新建專案打開 activity_main.xml 圖形化編輯器報錯:
Failed to find the style corresponding to the id 2130772026
android.support.v7.internal.app.WindowDecorActionBar 實例化失敗。

原因

ADT / Android Studio Layout Render 使用的 API Level 與專案 compileSdkVersion / targetSdkVersion 不匹配,導致主題樣式資源找不到。

解決步驟

  1. 打開佈局編輯器工具列。
  2. 找到綠色機器人圖示下拉選單:Android version to use when rendering layouts in the IDE。
  3. 選擇 **API 20 (Android 4.4W)** 或 **比 compileSdkVersion 低一級的版本**。
  4. 或直接在 styles.xml 將 parent="Theme.AppCompat.Light.DarkActionBar" 改為具體版本 parent="Base.Theme.AppCompat.Light.DarkActionBar"。

永久修正 (build.gradle)

android {
    compileSdkVersion 34
    defaultConfig {
        targetSdkVersion 34
        minSdkVersion 21
    }
}
dependencies {
    implementation 'androidx.appcompat:appcompat:1.6.1' // 遷移至 AndroidX
}

新專案建議直接使用 androidx 而非 android.support.v7。

驗證

切換 API Level 後,圖形化編輯器應正常顯示手機螢幕預覽,可拖拽元件。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved