Android -tag добавляет дополнительную маржу CardView - PullRequest
0 голосов
/ 03 августа 2020

Я сейчас работаю над школьным проектом, ориентированным на дизайн, в студии Android (Java код не является приоритетом), где я хотел разработать своего рода приложение для тренировок. Однако в настоящее время у меня возникают проблемы с тем, что мои XML -клейки не отображаются так же, как в Android Studio, поскольку они находятся в эмуляторе. Это даже несмотря на то, что и эмулятор, и студия Android используют Pixel 3 XL в качестве основы.

Я хочу включить макет, который я сделал из CardView, во фрагмент. Я делаю это, потому что позже хочу повторно использовать один и тот же CardView для нескольких разных фрагментов. Я включил макет в тег include layout = "" - и внутри этого тега установил поля (16dp), которые я хочу для CardView (см. Код ниже). Это выглядит совершенно нормально в предварительном просмотре Android Studio, но при запуске на эмуляторе похоже, что поле установлено на 48dp или что-то подобное (см. Изображения ниже).

Fragment Layout

<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=".ui.home.HomeFragment">

    <include layout="@layout/personinfo_cardview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="16dp" />

</LinearLayout>

Макет CardView

<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/personInfo_cardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:elevation="2dp"
android:padding="16dp"
app:cardCornerRadius="0dp">

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingTop="16dp"
    android:paddingBottom="16dp">

    <TextView
        android:id="@+id/weightValue_textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/value_unknown"
        android:textSize="18sp"
        app:layout_constraintBottom_toTopOf="@id/weightHeading_textView"
        app:layout_constraintEnd_toStartOf="@+id/heightValue_textView"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/heightValue_textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/value_unknown"
        android:textSize="18sp"
        app:layout_constraintBottom_toTopOf="@id/heightHeading_textView"
        app:layout_constraintEnd_toStartOf="@+id/ageValue_textView"
        app:layout_constraintStart_toEndOf="@id/weightValue_textView"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/ageValue_textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/value_unknown"
        android:textSize="18sp"
        app:layout_constraintBottom_toTopOf="@id/ageHeading_textView"
        app:layout_constraintEnd_toStartOf="@+id/genderValue_textView"
        app:layout_constraintStart_toEndOf="@id/heightValue_textView"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/genderValue_textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/value_unknown"
        android:textSize="18sp"
        app:layout_constraintBottom_toTopOf="@id/genderHeading_textView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@id/ageValue_textView"
        app:layout_constraintTop_toTopOf="parent" />


    <TextView
        android:id="@+id/weightHeading_textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/kilogram_short"
        android:textSize="14sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/heightValue_textView"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/weightValue_textView" />

    <TextView
        android:id="@+id/heightHeading_textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/centimeter_short"
        android:textSize="14sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/ageValue_textView"
        app:layout_constraintStart_toEndOf="@id/weightValue_textView"
        app:layout_constraintTop_toBottomOf="@id/heightValue_textView" />

    <TextView
        android:id="@+id/ageHeading_textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/age_short"
        android:textSize="14sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/genderValue_textView"
        app:layout_constraintStart_toEndOf="@id/heightValue_textView"
        app:layout_constraintTop_toBottomOf="@id/ageValue_textView" />

    <TextView
        android:id="@+id/genderHeading_textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/gender_short"
        android:textSize="14sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@id/ageValue_textView"
        app:layout_constraintTop_toBottomOf="@id/genderValue_textView" />

</androidx.constraintlayout.widget.ConstraintLayout>

img Android Studio preview

img Pixel 3 XL эмулятор

Что здесь происходит? Пытался изменить поля как внутри Layout-файла, так и во включаемом теге, но ничего не изменилось. Я также пробовал LinearLayout и ConstraintLayout в качестве оболочки, но с тем же результатом.

Любая помощь приветствуется!

Ответы [ 2 ]

0 голосов
/ 03 августа 2020

Оказалось, что это был nav_host_fragment, который имел какие-то странные поля. Это отвечает на мой вопрос: { ссылка }

0 голосов
/ 03 августа 2020

Удалите отступы / поля на CardView, include, ConstraintLayout. установить для cardView высоту app:cardElevation="0dp"

...