2017-02-22 14 views
1

я нашел здесь ответа на мой первый вопрос, и мне удалось установить мои две кнопки рядом друг с другом: XML файл:кнопки рядом друг с другом - но каждый из них имеет свою собственную сторону

<LinearLayout 
    android:id="@+id/linearLayout1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:layout_alignParentTop="false" 
    android:layout_alignParentLeft="false" 
    android:layout_alignParentStart="false" 
    android:layout_alignParentBottom="false" 
    android:layout_alignParentRight="false" 
    android:layout_centerHorizontal="true" 
    android:gravity="center" 
    android:layout_below="@id/description" 
    android:layout_alignParentEnd="false"> 

    <!-- Send it Button --> 
    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/confirm_it" 
     android:textColor="@color/white" 
     android:textSize="15sp" 
     android:layout_weight="0" 
     android:id="@+id/confirm_button" 
     android:backgroundTint="@color/darkCyan" 
     android:onClick="sendName" 
     /> 

    <!-- Skip it Button --> 
    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/skip_it" 
     android:textColor="@color/white" 
     android:textSize="15sp" 
     android:id="@+id/skip_button" 
     android:backgroundTint="@color/darkCyan" 
     android:layout_marginLeft="10dp" 
     android:layout_marginStart="10dp" 
     android:onClick="skipName" 
     /> 
    </LinearLayout> 

прямо сейчас Я хочу сделать что-то еще, я все еще хочу, чтобы две кнопки были рядом друг с другом, горизонтально, но я хочу, чтобы каждая из них, например, кнопка SEND слева от страницы, и кнопка SKIP справа от страницы:

Вот что я хочу:

enter image description here

Возможно ли это? и как?

ответ

0

Самый простой ответ заключается в замене LinearLayout с RelativeLayout и использовать Alignment

<RelativeLayout ...> 

    <Button ... 
     android:layout_alignParentLeft="true" /> 
    <Button ... 
     android:layout_alignParentRight="true" /> 

</RelativeLayout> 
+0

Только то, что мне нужно. Спасибо! – morkuk

0

ответ Ник Кардозо это правильно и чисто, и я лично хотел бы использовать этот метод. Если по какой-то причине вам абсолютно необходима LinearLayout вы можете добиться того, что вы спрашиваете следующим образом:

  1. Добавить пустой Вид между вашими двумя кнопками (с 0dp шириной и 0dp высота).
  2. Установить его "layout_weight" property to .

Вот код, чтобы помочь вам

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

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

    <View 
     android:layout_width="0dp" 
     android:layout_height="0dp" 
     android:layout_weight="1" /> 

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

</LinearLayout> 

Вот результат, который вы получаете с этим кодом:

Click for Image!

+0

Спасибо, Клафф, я буду использовать его, и я согласен, в моем проекте иногда мне нужен LinearLayout, поэтому он будет полезен. – morkuk

0

Если вы должны использовать LinearLayout. Вы можете попробовать LinearLayout с весом или weightSum

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

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

    <Button 
     android:layout_width="wrap_content" 
     android:layout_weight="1" 
     android:layout_height="wrap_content"/> 
</LinearLayout> 

или

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

    <Button 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight=".5"/> 

    <Button 
     android:layout_width="0dp" 
     android:layout_weight=".5" 
     android:layout_height="wrap_content"/> 
</LinearLayout> 
0

Есть более простой способ. Если вы добавляете макет между двумя кнопками, и если вес макета равен «1». Две кнопки выравниваются по желанию.

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <LinearLayout 
     android:orientation="horizontal" 
     android:minWidth="25px" 
     android:minHeight="25px" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/linearLayout1"> 
     <Button 
      android:text="Button" 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:id="@+id/button1" /> 
     <LinearLayout 
      android:orientation="horizontal" 
      android:minWidth="25px" 
      android:minHeight="25px" 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:id="@+id/linearLayout2" 
      android:layout_weight="1" /> <!-- I say there --> 
     <Button 
      android:text="Button" 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:id="@+id/button2" /> 
    </LinearLayout> 
</LinearLayout> 
Смежные вопросы