Как выровнять 3 вида по горизонтали в LinearLayout или другом макете - PullRequest
0 голосов
/ 17 декабря 2018

Я хочу выровнять 3 вида по горизонтали в одну строку a CheckBox, EditText и ImageButton.Я пытался сделать все возможное, но я не смог правильно реализовать. Я вставил приведенный ниже код XML, пожалуйста, дайте несколько советов или пример кода. Пожалуйста, помогите мне Спасибо заранее

enter image description here

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="match_parent">

    <LinearLayout
        android:id="@+id/parentLinear"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:id="@+id/linear"
            android:padding="5"
            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_marginTop="8dp"
                android:layout_marginBottom="8dp" />

            <EditText
                android:id="@+id/editText"
                android:layout_width="match_parent"
                android:layout_height="43dp"
                android:layout_marginStart="8dp"
                android:layout_marginTop="8dp"
                android:layout_marginEnd="8dp"
                android:layout_marginBottom="8dp"
                android:ems="10"
                android:inputType="textPersonName"
                android:text="Name" />
        </LinearLayout>
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/child"
        android:layout_alignRight="@id/parentLinear">
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <ImageButton
            android:id="@+id/imageButton2"
            android:layout_width="37dp"
            android:layout_height="40dp"
            android:layout_marginTop="5dp"
            android:layout_marginRight="5dp"
            android:background="?attr/selectableItemBackgroundBorderless"
            android:scaleType="centerInside"
            app:srcCompat="@drawable/add_blue" />
    </LinearLayout>
    </LinearLayout>
</RelativeLayout>

Ответы [ 6 ]

0 голосов
/ 17 декабря 2018

На самом деле, вам не нужно вложенное LinearLayout.Одного LinearLayout достаточно, чтобы удовлетворить ваши требования.Попробуйте следующий код ...

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:id="@+id/parentLinear"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto">


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

  <EditText
      android:id="@+id/editText"
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_marginStart="8dp"
      android:layout_marginTop="8dp"
      android:layout_marginEnd="8dp"
      android:layout_marginBottom="8dp"
      android:ems="10"
      android:inputType="textPersonName"
      android:text="Name"
      android:layout_weight="1"/>

  <ImageButton
      android:id="@+id/imageButton2"
      android:layout_width="37dp"
      android:layout_height="40dp"
      android:layout_marginTop="5dp"
      android:layout_marginRight="5dp"
      android:background="?attr/selectableItemBackgroundBorderless"
      android:scaleType="centerInside"
      app:srcCompat="@drawable/add_blue" />
</LinearLayout>

Попробуйте использовать как можно меньше макетов.

0 голосов
/ 17 декабря 2018

Проверьте ниже

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

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

<EditText
        android:contentDescription="@null"
        android:id="@+id/editText"
        android:layout_width="0dp"
        android:layout_height="43dp"
        android:layout_weight="1"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp"
        android:ems="10"
        android:inputType="textPersonName"
        android:text="Name"/>

<ImageButton
        android:contentDescription="@null"
        android:src="@drawable/ic_launcher_round"
        android:id="@+id/imageButton2"
        android:layout_width="37dp"
        android:layout_height="40dp"
        android:background="?attr/selectableItemBackgroundBorderless"
        android:scaleType="centerInside"
/>

enter image description here

0 голосов
/ 17 декабря 2018

Для LinearLayout вы должны использовать android:orientation="horizontal".Для ConstraintLayout вы должны использовать app:layout_constraintHorizontal_weight="value like 1, 2", и для этого вы должны установить RightToLeftOf и LegtToRightOf для обоих представлений.Для RelativeLayout Вы должны установить alignSTartToEndOf в представлении вашего ребенка

0 голосов
/ 17 декабря 2018
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
    android:id="@+id/parentLinear"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <CheckBox
        android:id="@+id/checkBox"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginTop="8dp"
        android:layout_weight="0.1" />

    <EditText
        android:id="@+id/editText"
        android:layout_width="0dp"
        android:layout_weight="0.8"
        android:layout_height="43dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:ems="10"
        android:inputType="textPersonName"
        android:text="Name" />

    <ImageButton
        android:id="@+id/imageButton2"
        android:layout_width="0dp"
        android:layout_weight="0.1"
        android:layout_height="40dp"
        android:layout_marginRight="5dp"
        android:layout_marginTop="5dp"
        android:background="?attr/selectableItemBackgroundBorderless"
        android:scaleType="centerInside"
        android:src="@drawable/add_blue" />
</LinearLayout>
</RelativeLayout>
0 голосов
/ 17 декабря 2018

Вместо использования линейного макета было бы лучше, если бы вы использовали либо RELATIVE LAYOUT, либо Constraint Layout (лучше).Это было бы легче сделать и будет работать лучше.или используйте один линейный макет для выравнивания видов, и вы используете слишком много линейных макетов, что будет очень дорогой операцией.

0 голосов
/ 17 декабря 2018

Нет необходимости использовать вложенный макет для этого

Вы можете достичь этого, используя только один LinearLayout

Вы можете установить android:drawableEnd="@drawable/ic_fav" в вашем EditText, чтобы заменить кнопку изображения

КОД ОБРАЗЦА

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

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

    <EditText
        android:id="@+id/editText"
        android:layout_width="match_parent"
        android:layout_height="43dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        android:drawableEnd="@drawable/ic_fav"
        android:ems="10"
        android:inputType="textPersonName"
        android:text="Name" />

</LinearLayout>

ВЫХОД

enter image description here

Другой способ

, если вы не хотите использовать android:drawableEnd, чем здесь второе решение

<?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:id="@+id/coordinatorlayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

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

    <RelativeLayout
        android:layout_width="match_parent"
        android:background="@drawable/test"
        android:layout_height="wrap_content">

        <EditText
            android:id="@+id/editText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginBottom="8dp"
            android:ems="10"
            android:background="@android:color/transparent"
            android:inputType="textPersonName"
            android:text="Name" />

        <ImageButton
            android:id="@+id/imageButton2"
            android:layout_width="37dp"
            android:layout_alignParentEnd="true"
            android:layout_height="40dp"
            android:layout_marginTop="5dp"
            android:layout_marginRight="5dp"
            android:background="?attr/selectableItemBackgroundBorderless"
            android:scaleType="centerInside"
            app:srcCompat="@drawable/ic_fav" />

    </RelativeLayout>

</LinearLayout>

Drawable / Test

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:top="-10dp"
        android:left="-10dp"
        android:right="-10dp">
        <shape android:shape="rectangle">
            <solid android:color="@android:color/transparent"/>

            <stroke android:width="2dp"
                android:color="#3498db"/>
        </shape>
    </item>
</layer-list>

ВЫХОД

enter image description here

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