#android #xml
#Android #xml
Вопрос:
Итак, я попробовал следующий код
<?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:AndroidProjectsTutorialsCustomDesignappsrcmainreslayoutactivity_main.xml
Error:(17, 31) No resource found that matches the given name (at 'layout_above' with value '@id/textView').
E:AndroidProjectsTutorialsCustomDesignappbuildintermediatesresmergeddebuglayoutactivity_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>
Я не понимаю, почему первое не работает. Неправильно ли упорядочивать различные элементы на основе элемента, который находится в центре макета.
Ответ №1:
Не найдено ресурса, соответствующего данному имени (в ‘layout_above’ со значением ‘@id / TextView’).
Он пытается сообщить вам, что вы используете идентификатор, который еще не был инициализирован. Чтобы избежать этого, вы должны иметь возможность использовать первый макет, если вы укажете «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" />
Ответ №2:
Потому что в первой строке android:layout_above=»@id /TextView»
<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), который еще не был прочитан из анализатора xml. Просто измените порядок: (сначала textview, затем imageview в xml)
PS: tools:context="com.slidenerd.sample.customdesign.MainActivity"
не нужно, потому что это не ваш проект