TextView между двумя элементами - PullRequest
0 голосов
/ 05 сентября 2018

У меня есть приложение для Android, и я создал вид. Это представление содержит RelativeLayout и содержит 3 дочерних элемента: CheckBox, TextView и Button. CheckBox находится слева от родительского элемента, кнопка находится справа, а TextView между этими элементами. TextView может содержать текст произвольной длины, и если текст очень длинный, TextView не должен перекрывать ни ChecBox, ни Button. Теперь это выглядит так:

enter image description here

Но я хочу это:

enter image description here

Можно ли правильно разместить элементы?

Ответы [ 8 ]

0 голосов
/ 05 сентября 2018

Не нужно ставить Textview между checkBox и Button , Вы можете сделать с 2-мя элементами (CheckBox и Button). что ты должен сделать просто установите текст флажок , а затем установите кнопку.

Для лучшего понимания прочитайте следующий код:

<LinearLayout
    android:gravity="center"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <CheckBox
        android:text="Some bla blablablablablablabla bla bla bla bla bla bla "
        android:layout_width="0dp"
        android:layout_weight="0.7"

        android:layout_height="wrap_content" />
    <Button
        android:text="Button"
        android:layout_width="0dp"
        android:layout_weight="0.3"
        android:layout_height="wrap_content" />
</LinearLayout>
0 голосов
/ 05 сентября 2018

Используя ConstraintLayout, вы можете получить это:

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

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

            <CheckBox
                android:id="@+id/checkBox"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginBottom="8dp"
                app:layout_constraintBottom_toBottomOf="parent" />

            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:text="this is a long text bla bla bla la this is a long text bla bla bla la"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toStartOf="@+id/button2"
                app:layout_constraintStart_toEndOf="@+id/checkBox"
                app:layout_constraintTop_toTopOf="parent" />

            <Button
                android:id="@+id/button2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginEnd="8dp"
                android:text="Button"
                app:layout_constraintEnd_toEndOf="parent" />


        </android.support.constraint.ConstraintLayout>
    </LinearLayout>

Результат: enter image description here

0 голосов
/ 05 сентября 2018

Попробуйте этот XML-макет:

<?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"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="8dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

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

        <CheckBox
            android:id="@+id/checkBox"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="8dp"
            app:layout_constraintBottom_toBottomOf="parent" />

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="Some Long text Bla Bla Bla Bla Bla Bla Bla Bla Bla Bla Bla Bla Bla Bla Bla Bla Bla Bla Bla Bla "
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toStartOf="@+id/button2"
            app:layout_constraintStart_toEndOf="@+id/checkBox"
            app:layout_constraintTop_toTopOf="parent" />

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="8dp"
            android:text="Button"
            app:layout_constraintEnd_toEndOf="parent" />    
    </android.support.constraint.ConstraintLayout>
</LinearLayout>

enter image description here

Надеюсь, это поможет вам.

0 голосов
/ 05 сентября 2018

Вы можете использовать это

 <RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <CheckBox
        android:id="@+id/checkBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_centerVertical="true" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:text="button" />

    <TextView
        android:text="this is my test this is my test this is my test this is my test this is my test "
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_toEndOf="@id/checkBox"
        android:layout_toLeftOf="@id/button"
        android:layout_toRightOf="@id/checkBox"
        android:layout_toStartOf="@id/button" />

</RelativeLayout>

enter image description here

0 голосов
/ 05 сентября 2018

Вы можете просто добавить weight к просмотру текста, оно автоматически настроится

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:gravity="center_vertical" android:layout_width="match_parent"
    android:orientation="horizontal"
    android:layout_height="wrap_content">

    <CheckBox
        android:id="@+id/checkBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"   />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="large text view that not overlay neither checkbox nor button" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />
</LinearLayout>
0 голосов
/ 05 сентября 2018

a linearLayout (вместоlativelayout), заключающий в себе 3 представления, должен сделать свое дело. просто не забудьте установить вес textview в 1 и ширину в 0

<LinearLayout  
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<CheckBox 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"  />

<TextView 
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:weight="1"  />

<Button 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"  />

</LinearLayout>
0 голосов
/ 05 сентября 2018

enter image description here

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <CheckBox
        android:id="@+id/checkBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:layout_toRightOf="@id/checkBox"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Loren Emsim Loren Emsim Loren Emsim Loren Emsim Loren Emsim Loren Emsim Loren Emsim Loren Emsim Loren Emsim Loren Emsim Loren Emsim Loren Emsim"
        android:layout_toLeftOf="@id/btn" />

    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:text="button"
        android:layout_alignParentRight="true" />

</RelativeLayout>
0 голосов
/ 05 сентября 2018

Вы можете сделать это так:

<?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">

<CheckBox
    android:id="@+id/checkBox"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="8dp"
    android:layout_marginStart="8dp"
    app:layout_constraintBottom_toBottomOf="@+id/textView"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="@+id/textView" />

<TextView
    android:id="@+id/textView"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:text="Helovusndijfsdufsdjfkjdsbfkjsdkjfbskjdbfkjsbdfkjbskdjfbskjbdfkjbfsdf"
    app:layout_constraintBottom_toBottomOf="@+id/button"
    app:layout_constraintEnd_toStartOf="@+id/button"
    app:layout_constraintHorizontal_bias="0.005"
    app:layout_constraintStart_toEndOf="@+id/checkBox"
    app:layout_constraintTop_toTopOf="@+id/button" />

<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginTop="8dp"
    android:text="Button"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

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

enter image description here

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...