Как установить высоту ячейки Android ListView как половину ширины ячейки? - PullRequest
0 голосов
/ 17 апреля 2019

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

<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/content"

    android:layout_height="0dp"
    android:layout_width="match_parent"> 

        <!-- some content inside -->

</android.support.constraint.ConstraintLayout>

. Тогда я думаю, что это может быть из-за того, что я не установил width="0dp" (пусть ограничение определяет ширину).Поэтому я попробовал другой способ, как показано ниже.Но тогда ширина станет нулевой.

Как правильно это сделать?

<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/content"

    android:layout_height="0dp"
    android:layout_width="0dp"
    app:layout_constraintStart_toEndOf="parent"
    app:layout_constraintEnd_toEndOf="parent"> 

        <!-- some content inside -->

</android.support.constraint.ConstraintLayout>

РЕДАКТИРОВАТЬ : рабочее решение, вдохновленное ответом Anddenver(с модификацией):

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <FrameLayout
        android:background="@android:color/holo_green_dark"
        android:layout_width="0dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        android:layout_height="0dp"
        app:layout_constraintDimensionRatio="2:1">



    </FrameLayout>


</android.support.constraint.ConstraintLayout>

Дальнейшее редактирование:

Кажется, мне нужно обернуть еще один слой макета ограничений для моего внутреннего содержимого ....:

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <FrameLayout
        android:background="@android:color/holo_green_dark"
        android:layout_width="0dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        android:layout_height="0dp"
        app:layout_constraintDimensionRatio="2:1">

        <android.support.constraint.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">


            <actual contents here..../>

        </android.support.constraint.ConstraintLayout>




    </FrameLayout>


</android.support.constraint.ConstraintLayout>

Или просто удалите FrameLayout и оберните ConstraintLayout внутри ConstraintLayout:

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <android.support.constraint.ConstraintLayout
        android:background="@android:color/holo_green_dark"
        android:layout_width="0dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        android:layout_height="0dp"
        app:layout_constraintDimensionRatio="2:1">

contents here...

        </android.support.constraint.ConstraintLayout>

</android.support.constraint.ConstraintLayout>

Ответы [ 2 ]

2 голосов
/ 17 апреля 2019

Для этого вам нужен внутренний вид, например:

<android.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

<FrameLayout
        android:background="@color/colorPrimary"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintDimensionRatio="1:1">

    <YOUR CONTENT>

</FrameLayout>

dimensionRatio screenshot

1 голос
/ 17 апреля 2019

Для ListView вам нужно накачать пользовательский list_item.xml

, и это должно быть H,2:1, чтобы ограничить высоту соотношением сторон 2:1.

По умолчанию android.R.layout.simple_list_item_1 это просто TextView:

<?xml version="1.0" encoding="utf-8"?>
<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    android:layout_height="0dp"
    android:layout_width="0dp"
    app:layout_constraintDimensionRatio="H,2:1"
    ...
    />

см. DimensionConstraints .

...