首页 > 解决方案 > Android Studio:“无法执行重构。无法重命名此元素”

问题描述

我想更改图形的名称,但android studio显示错误:

“无法执行重构。无法重命名此元素”

左边是一个教程,右边是我的代码 在此处输入图像描述

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/name_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/name"
        android:textAlignment="center"
        style="@style/NameStyle" />

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:srcCompat="@android:drawable/btn_star_big_on"
        />
</LinearLayout>

重构仅在id窗口中起作用:

在此处输入图像描述

为什么它在 XML 代码中不起作用?

这是 Kubuntu 20.04 LTS 上的第一个项目。

标签: android

解决方案


您正在尝试重命名 android 平台的内置可绘制对象,而您发布的教程屏幕截图正在尝试重命名视图的 id

我的意思是

具有@android:as 前缀的资源是在 android 平台中构建的资源,因此无法重构或重命名,并且您的屏幕截图正试图像这样重命名。您可以看到对话框标题Rename xyz and its occurence to,指示您尝试重命名的元素

 <ImageView
        android:id="@+id/imageView" // This is being changed in the screenshot
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:srcCompat="@android:drawable/btn_star_big_on" // You are trying to change this

推荐阅读