gpt4 book ai didi

android - 在有关布局 xml 文件的简单 Android 应用程序中出现错误

转载 作者:行者123 更新时间:2023-11-29 17:13:25 25 4
gpt4 key购买 nike

所以我尝试了下面的代码

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.slidenerd.sample.customdesign.MainActivity"
android:background="#252525">


<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/clock"
android:layout_above="@id/textView"
android:id="@+id/imageView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />

<TextView
android:text="0:00"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView"
android:textColor="#616161"
android:textSize="100sp"
android:layout_centerInParent="true" />

<ToggleButton
android:text="ToggleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView"
android:layout_centerHorizontal="true"
android:layout_marginTop="54dp"
android:id="@+id/toggleButton"
android:background="@drawable/toggle_selector"
android:textOff=""
android:textOn=""/>
</RelativeLayout>

我得到的错误是这些

Information:Gradle tasks [:app:clean, :app:generateDebugSources, :app:generateDebugAndroidTestSources, :app:mockableAndroidJar, :app:prepareDebugUnitTestDependencies, :app:assembleDebug]
E:\AndroidProjects\Tutorials\CustomDesign\app\src\main\res\layout\activity_main.xml
Error:(17, 31) No resource found that matches the given name (at 'layout_above' with value '@id/textView').
E:\AndroidProjects\Tutorials\CustomDesign\app\build\intermediates\res\merged\debug\layout\activity_main.xml
Error:(17, 31) No resource found that matches the given name (at 'layout_above' with value '@id/textView').
Error:Execution failed for task ':app:processDebugResources'.
> com.android.ide.common.process.ProcessException: Failed to execute aapt
Information:BUILD FAILED
Information:Total time: 21.791 secs
Information:3 errors
Information:0 warnings
Information:See complete output in console

相比之下,下面的工作正常

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.slidenerd.sample.customdesign.MainActivity"
android:background="#252525">


<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/clock"
android:layout_marginTop="54dp"
android:id="@+id/imageView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />

<TextView
android:text="0:00"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/imageView"
android:id="@+id/textView"
android:textColor="#616161"
android:textSize="100sp"
android:layout_centerInParent="true" />

<ToggleButton
android:text="ToggleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView"
android:layout_centerHorizontal="true"
android:layout_marginTop="54dp"
android:id="@+id/toggleButton"
android:background="@drawable/toggle_selector"
android:textOff=""
android:textOn=""/>
</RelativeLayout>

我不明白为什么第一个不起作用。根据放置在布局中心的项目来排列各种项目是否错误。

最佳答案

No resource found that matches the given name (at 'layout_above' with value '@id/textView').

它试图告诉您您正在使用尚未初始化的 ID。为避免这种情况,您应该能够使用第一个布局,只要您指定“android:layout_above="@+id/textView"(@+id 中需要有“+”)

    <ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/clock"
android:layout_above="@id/textView" <-- this guy
android:id="@+id/imageView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />

关于android - 在有关布局 xml 文件的简单 Android 应用程序中出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39922955/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com