Автоподбор элементов в линейном макете - PullRequest
2 голосов
/ 30 сентября 2019

У меня есть два макета ограничений в линейном макете. Я хочу, чтобы макеты ограничений занимали 30% и 70% экрана соответственно на всех устройствах. Какой атрибут внутри линейного макета мне нужно установить, чтобы добиться этого?

Ответы [ 4 ]

0 голосов
/ 01 октября 2019

Это должно дать желаемый результат. Установка атрибута weightSum для родительского линейного макета и соответствующая присвоение весов помогает

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:weightSum="1" >

        <android.support.constraint.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="0.3"/>

        <android.support.constraint.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="0.7"/>
</LinearLayout>
0 голосов
/ 30 сентября 2019

Используйте атрибут WeightSum в LinearLayout для получения ожидаемого результата, что проще, чем другие методы.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="3" > 


<android.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="3">

  //Child layouts on 30% of layout

</android.support.constraint.ConstraintLayout>


<android.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="7">

  //Child layouts on 70% of layout

</android.support.constraint.ConstraintLayout>
0 голосов
/ 30 сентября 2019

Если вы хотите сделать что-то подобное, используйте

 android:layout_weight="any number"

, но вы должны сделать одинаковую высоту для них обоих , не имеет значения, будет ли высота wrap_content или match_parent макет будет занимать больше формы экрана, если номер меньше другого

0 голосов
/ 30 сентября 2019

Вы должны принять Parent Layout как ConstraintLayout, а затем после того, как вам нужно будет использовать GuideLine, чтобы установить 30 и 70 процентов экрана.

<?xml version="1.0" encoding="utf-8"?><?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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">

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.3" />

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@android:color/holo_green_light"
        app:layout_constraintBottom_toTopOf="@+id/guideline"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@android:color/darker_gray"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/guideline" />
</androidx.constraintlayout.widget.ConstraintLayout>

Выход

enter image description here

...