Почему элементы XML не видны на моем эмуляторе в Android Studio? - PullRequest
0 голосов
/ 29 августа 2018

Моя проблема в том, что размещенные мной элементы управления (флажки, кнопки и т. Д.) Не видны в моем эмуляторе. Ниже мой XML-файл:

 <?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<Button
    android:id="@+id/btn1"
    android:layout_width="match_parent"
    android:layout_height="52dp"
    android:text="@string/btn1"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="1.0"
    app:layout_constraintStart_toStartOf="parent"
    tools:ignore="MissingConstraints"
    tools:layout_editor_absoluteY="0dp" />

<Button
    android:id="@+id/btn2"
    android:layout_width="match_parent"
    android:layout_height="77dp"
    android:text="@string/btn2"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="parent"
    tools:ignore="MissingConstraints"
    tools:layout_editor_absoluteY="68dp" />

<fragment
    android:id="@+id/fragment"
    android:name="com.example.mypc.fragmentactivity.Fragment1"
    android:layout_width="222dp"
    android:layout_height="285dp"
    android:layout_marginBottom="40dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    tools:ignore="MissingConstraints" />

Вот мой XML (представление дизайна): XML (design view)

Вот мое окно эмулятора: emulator window

Что-то не так в моем XML-файле?

Пожалуйста, предоставьте мне решение для этого. Как мне справиться с этим?

Ответы [ 5 ]

0 голосов
/ 29 августа 2018

Вы используете ConstraintLayout без относительного позиционирования .

По данным сайта разработчика:

Относительное расположение является одним из основных строительных блоков для создания макетов в ConstraintLayout. Эти ограничения позволяют вам позиционировать данный виджет относительно другого. Вы можете ограничить виджет по горизонтальной и вертикальной оси:

Горизонтальная ось: влево, вправо, начало и конец

Вертикальная ось: верх, низ и боковая линия текста

Вот решение, которое может вам помочь:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/btn1"
        android:layout_width="match_parent"
        android:layout_height="52dp"
        android:text="@string/btn1"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toBottomOf="@id/btn2"/>

    <Button
        android:id="@+id/btn2"
        android:layout_width="match_parent"
        android:layout_height="77dp"
        android:text="@string/btn2"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/btn1"
        android:layout_marginTop="10dp"/>


</android.support.constraint.ConstraintLayout>

Я использовал относительное расположение для решения проблемы. Если вы используете макет ограничения, то без относительного позиционирования ваши представления будут перекрываться друг с другом.

0 голосов
/ 29 августа 2018

В вашем коде ваши элементы переполняют друг друга в одной позиции, поэтому они не видны.

Мое предложение не ограничивать Layout на начальном этапе обучения Android. Используйте Linear Layout вместо Layout Layout ограничения.

Поскольку в редакторе макетов Layout ограничения используются ограничения для определения положения элемента пользовательского интерфейса в макете. Вы должны задать множество XML-атрибутов ограничения, например.

app:layout_editor_absoluteX="73dp"
        app:layout_editor_absoluteY="176dp"
        app:layout_constraintLeft_creator="1"
        app:layout_constraintTop_creator="1"
        app:layout_constraintRight_creator="1"

для указания положения элементов в макете.

Используйте Linear Layout вместо Constraint Layout и дайте

 orientation:"vetical"

для просмотра всех ваших элементов.

0 голосов
/ 29 августа 2018

Потому что вы не правильно выровняли вторую кнопку. похоже, что вторая кнопка перекрывает первую.

попробуйте это

<android.support.constraint.ConstraintLayout 
    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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".activites.HomeActivity"
    android:layout_margin="6dp">

    <Button
        android:id="@+id/btnOne"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text="Button One"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/btnTwo"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text="Button Two"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/btnOne" />

    <fragment
        android:id="@+id/fragment"
        android:layout_width="222dp"
        android:layout_height="285dp"
        android:layout_marginBottom="40dp"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/btnTwo"
        tools:ignore="MissingConstraints" />

</android.support.constraint.ConstraintLayout>

будет выглядеть так:

enter image description here

0 голосов
/ 29 августа 2018

Атрибуты layout_editor используются только для editor preview и не влияют на окончательные макеты. Вы должны расположить свои элементы, используя ConstraintLayout определенные атрибуты.

Используется только для предварительного просмотра.

tools:layout_editor_absoluteY="68dp"
0 голосов
/ 29 августа 2018

Просто используйте LinearLayout вместо ConstraintLayout. Также укажите вертикальную ориентацию LinearLayout. Это поможет вам

<?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"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical"
 tools:context=".MainActivity">

 <Button
    android:id="@+id/btn1"
    android:layout_width="match_parent"
    android:layout_height="52dp"
    android:text="@string/btn1" />

<Button
    android:id="@+id/btn2"
    android:layout_width="match_parent"
    android:layout_height="77dp"
    android:text="@string/btn2" />

<fragment
    android:id="@+id/fragment"
    android:name="com.example.mypc.fragmentactivity.Fragment1"
    android:layout_width="222dp"
    android:layout_height="285dp"
    android:layout_marginBottom="40dp" />
</LinearLayout>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...