LinearLayout не может быть правильно выровнен - PullRequest
1 голос
/ 10 июня 2019

Привет, я хочу макет, как показано ниже

Main amount         120 usd
Extra charge        2   usd
----------------------------
Sub Total           122 usd
Discount             5  usd
----------------------------
Total amount        117 usd

Я совершенно не знаком с макетом ограничения. Я пытался использовать гравитацию / макет / выравнивание LinearLayout вправо для правой части, но это не работает, все выровнено по левому краю. Я буду рад, если кто-нибудь даст пример кода для этого макета, плз .

Обновление: мой текущий код

<LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
        <TextView
            android:layout_width="wrap_content"
            android:text="Main Amount"
            android:layout_height="wrap_content" />
        <TextView
            android:layout_width="wrap_content"
            android:gravity="left"
            android:layout_gravity="left"
            android:textAlignment="gravity"
            android:text="10 "
            android:layout_height="wrap_content" />
        <TextView
            android:layout_width="wrap_content"
            android:gravity="left"
            android:layout_gravity="left"
            android:textAlignment="gravity"
            android:text="USD"
            android:layout_height="match_parent" />
        </LinearLayout>

Ответы [ 4 ]

1 голос
/ 13 июня 2019

Для этого вида представления используйте LinearLayout со свойством weightSum для родительского LinearLayout и используйте layout_weight для Children LinearLayout

<LinearLayout 
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:weightSum="4">

   <LinearLayout
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="2">
       <TextView
         android:layout_width="wrap_content"
         android:text="Main Amount"
         android:layout_height="wrap_content" />
  </LinearLayout>
  <LinearLayout
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1">
        <TextView
            android:layout_width="wrap_content"
            android:gravity="left"
            android:layout_gravity="left"
            android:textAlignment="gravity"
            android:text="10 "
            android:layout_height="wrap_content" />
  </LinearLayout>
  <LinearLayout
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1">
        <TextView
            android:layout_width="wrap_content"
            android:gravity="left"
            android:layout_gravity="left"
            android:textAlignment="gravity"
            android:text="USD"
            android:layout_height="match_parent" />

   </LinearLayout>
</LinearLayout>
1 голос
/ 10 июня 2019

Вы можете попробовать что-то вроде этого: я использовал LinearLayout в качестве родителя и добавил RelativeLayout к нему, чтобы создать выравнивание TextViews так, как вы хотите.Вы можете дублировать Относительный макет, чтобы создать остальное.не уверен, что это лучший способ ... но он работает

<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=".MainActivity">

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

    <TextView
        android:id="@+id/main"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Main" />

    <TextView
        android:id="@+id/amount"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_toRightOf="@+id/main"
        android:text="amount" />

    <TextView
        android:id="@+id/extra"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Extra"
        android:layout_below="@+id/amount"
        />

    <TextView
        android:id="@+id/charge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_toRightOf="@+id/extra"
        android:layout_alignBottom="@+id/extra"
        android:text="charge" />

    <TextView
        android:id="@+id/usd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/main"
        android:layout_alignParentRight="true"
        android:text="usd"
        android:layout_marginRight="30dp"
        />

    <TextView
        android:id="@+id/num1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/main"
        android:layout_toLeftOf="@+id/usd"
        android:text="120"
        android:layout_marginRight="10dp"
        />

    <TextView
        android:id="@+id/usd2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/extra"
        android:layout_alignParentRight="true"
        android:text="usd"
        android:layout_marginRight="30dp"
        />

    <TextView
        android:id="@+id/num2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/extra"
        android:layout_toLeftOf="@+id/usd2"
        android:text="120"
        android:layout_marginRight="10dp"
        />

</RelativeLayout>

enter image description here

1 голос
/ 10 июня 2019

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

Кроме того, ваш макет будет реагировать на все размеры экрана, поэтому рассмотрите возможность использования ConstraintLyaout

Вот пример желаемого макета с использованием ConstraintLyaout:

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

<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:text="Button"
    app:layout_constraintBottom_toTopOf="@+id/button4"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button"
    app:layout_constraintBottom_toTopOf="@+id/button6"
    app:layout_constraintStart_toStartOf="@+id/button"
    app:layout_constraintTop_toBottomOf="@+id/button3" />

<Button
    android:id="@+id/button6"
    android:layout_width="0dp"
    android:layout_height="4dp"
    android:text="Button"
    android:background="@color/bronze"
    app:layout_constraintBottom_toTopOf="@+id/button7"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/button2" />

<Button
    android:id="@+id/button7"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintStart_toStartOf="@+id/button"
    app:layout_constraintTop_toBottomOf="@+id/button6" />

<Button
    android:id="@+id/button3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button"
    app:layout_constraintBottom_toTopOf="@+id/button2"
    app:layout_constraintStart_toStartOf="@+id/button"
    app:layout_constraintTop_toBottomOf="@+id/button5" />

<Button
    android:id="@+id/button4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button"
    app:layout_constraintBottom_toTopOf="@+id/button5"
    app:layout_constraintStart_toStartOf="@+id/button"
    app:layout_constraintTop_toBottomOf="@+id/button" />

<Button
    android:id="@+id/button5"
    android:layout_width="0dp"
    android:layout_height="4dp"
    android:text="Button"
    android:background="@color/bronze"
    app:layout_constraintBottom_toTopOf="@+id/button3"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/button4" />

<TextView
    android:id="@+id/textView11"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="8dp"
    android:text="TextView"
    app:layout_constraintBottom_toBottomOf="@+id/button"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="@+id/button" />

<TextView
    android:id="@+id/textView12"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="8dp"
    android:text="TextView"
    app:layout_constraintBottom_toBottomOf="@+id/button7"
    app:layout_constraintEnd_toEndOf="@+id/textView11"
    app:layout_constraintTop_toTopOf="@+id/button7" />

<TextView
    android:id="@+id/textView13"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="8dp"
    android:text="TextView"
    app:layout_constraintBottom_toBottomOf="@+id/button2"
    app:layout_constraintEnd_toEndOf="@+id/textView11"
    app:layout_constraintTop_toTopOf="@+id/button2" />

<TextView
    android:id="@+id/textView14"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="8dp"
    android:text="TextView"
    app:layout_constraintEnd_toEndOf="@+id/textView11"
    app:layout_constraintTop_toTopOf="@+id/button3" />

<TextView
    android:id="@+id/textView15"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView"
    app:layout_constraintBottom_toBottomOf="@+id/button4"
    app:layout_constraintEnd_toEndOf="@+id/textView11"
    app:layout_constraintTop_toTopOf="@+id/button4" />
</android.support.constraint.ConstraintLayout>

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

enter image description here

1 голос
/ 10 июня 2019

LinearLayout будет трудным без большого количества жесткого кодирования layout_width для детей.Вы уже пробовали TableLayout?

https://developer.android.com/reference/android/widget/TableLayout

TableLayout иногда может быть трудной для достижения совершенства, но он даст вам макет, который вы ищете.

...