가로 LinearLayout의 오른쪽 맞춤 버튼

첨부 된 이미지를 보면 버튼을 올바르게 정렬해야하지만 어떤 이유로 ‘gravity : right’와 함께 작동하지 않습니다 …

해당 레이아웃에 대한 내 코드는 다음과 같습니다.

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_marginTop="35dp">

    <TextView
        android:id="@+id/lblExpenseCancel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/cancel"
        android:textColor="#404040"
        android:layout_marginLeft="10dp"
        android:textSize="20sp"
        android:layout_marginTop="9dp" />

    <Button
        android:id="@+id/btnAddExpense"
        android:layout_width="wrap_content"
        android:layout_height="45dp"
        android:background="@drawable/stitch_button"
        android:layout_marginLeft="10dp"
        android:text="@string/add"
        android:layout_gravity="right"
        android:layout_marginRight="15dp" />

</LinearLayout>

왜 작동하지 않습니까?!



답변

아래 코드를 사용하십시오.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="35dp"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/lblExpenseCancel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="9dp"
        android:text="@string/cancel"
        android:textColor="#404040"
        android:textSize="20sp" />

    <Button
        android:id="@+id/btnAddExpense"
        android:layout_width="wrap_content"
        android:layout_height="45dp"
        android:layout_alignParentRight="true"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="15dp"
        android:background="@drawable/stitch_button"
        android:text="@string/add" />

</RelativeLayout>

답변

단일 LinearLayout솔루션. 간단한 간격보기 만 추가하십시오
.

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_marginTop="35dp">

    <TextView
        android:id="@+id/lblExpenseCancel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/cancel"
        android:textColor="#404040"
        android:layout_marginLeft="10dp"
        android:textSize="20sp"
        android:layout_marginTop="9dp" />

    <!-------------------------  ADDED SPACER VIEW -------------------->
    <View
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_weight="1"
        />
    <!------------------------- /ADDED SPACER VIEW -------------------->

    <Button
        android:id="@+id/btnAddExpense"
        android:layout_width="wrap_content"
        android:layout_height="45dp"
        android:background="@drawable/stitch_button"
        android:layout_marginLeft="10dp"
        android:text="@string/add"
        android:layout_gravity="right"
        android:layout_marginRight="15dp" />

</LinearLayout>

“강조 표시된”보기에 유의하십시오. 다른 사항은 수정하지 않았습니다. 높이가 0이면 보이지 않습니다. 너비 = 0은 너비 LinearLayout= 1을 기준으로 너비를 결정하기 때문 입니다. 이것은 스페이서 뷰가 방향 (방향)으로 가능한 한 많이 늘어나는 것을 의미합니다 LinearLayout.

참고 사용한다 android.widget.Space또는 android.support.v4.widget.Space대신View 하여 API 레벨 및 / 또는 의존성이 그것을 허용하는 경우. Space측정하는 것만으로는하지 않고, 더 싼 방법으로 작업을 View수행합니다. 또한 의도를보다 표현 적으로 전달합니다.


답변

그것을위한 실제 솔루션 :

android:layout_weight="1" TextView의 경우 버튼이 오른쪽으로 이동합니다!


답변

이 질문은 얼마 전에 요청 된 것을 알고 있지만 이전에이 작업을 수행했으며 항목을 정렬 할 때 더 많은 제어가 가능합니다. 웹 프로그래밍처럼 보이면 도움이됩니다. 당신 LinearLayout은 그 안에 버튼을 포함 할 다른 것을 중첩 시킵니다. 그런 다음 버튼 레이아웃의 중력을 변경하고 오른쪽에있는 동안TextView 가 여전히 왼쪽에있는 있습니다.

이 코드를 사용해보십시오 :

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="35dp">

<TextView
    android:id="@+id/lblExpenseCancel"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/cancel"
    android:textColor="#404040"
    android:layout_marginLeft="10dp"
    android:textSize="20sp"
    android:layout_marginTop="9dp"/>

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

    <Button
        android:id="@+id/btnAddExpense"
        android:layout_width="wrap_content"
        android:layout_height="45dp"
        android:background="@drawable/stitch_button"
        android:layout_marginLeft="10dp"
        android:text="@string/add"
        android:layout_gravity="right"
        android:layout_marginRight="15dp"/>

</LinearLayout>

중첩 된 레이아웃에서 패딩을 사용하여 원하는 방식으로 작동해야 할 수도 있습니다.


답변

이거 한번 해봐

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

  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:orientation="horizontal" >

<TextView
    android:id="@+id/lblExpenseCancel"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="10dp"
    android:layout_marginTop="9dp"
    android:text="cancel"
    android:textColor="#ffff0000"
    android:textSize="20sp" />

<Button
    android:id="@+id/btnAddExpense"
    android:layout_width="wrap_content"
    android:layout_height="45dp"
    android:layout_alignParentRight="true"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="15dp"
     android:textColor="#ff0000ff"
    android:text="add" />

 </RelativeLayout>
  </LinearLayout>

답변

몇 번 전에 언급했듯이 LinearLayout에서 RelativeLayout으로 전환하면 작동하지만 문제를 피하는 대신 해결할 수도 있습니다. LinearLayout이 제공하는 도구를 사용하십시오. TextView에 weight = 1 (아래 코드 참조)을 지정하십시오. 단추의 가중치는 0으로 유지되거나 없어야합니다. 이 경우 TextView는 나머지 공간을 모두 차지하므로 TextView 또는 ButtonView의 내용을 표시하는 데 사용되지 않고 버튼을 오른쪽으로 밉니다.

<LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="35dp">

        <TextView
            android:id="@+id/lblExpenseCancel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/cancel"
            android:textColor="#404040"
            android:layout_marginLeft="10dp"
            android:textSize="20sp"
            android:layout_marginTop="9dp"

            **android:layout_weight="1"**

            />

        <Button
            android:id="@+id/btnAddExpense"
            android:layout_width="wrap_content"
            android:layout_height="45dp"
            android:background="@drawable/stitch_button"
            android:layout_marginLeft="10dp"
            android:text="@string/add"
            android:layout_gravity="right"
            android:layout_marginRight="15dp" />

    </LinearLayout>

답변

버튼이 아닌 레이아웃에 중력을 추가해야합니다. 버튼 설정의 중력은 버튼 내부의 텍스트입니다

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_gravity="right"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="35dp">