가로 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
수행합니다. 또한 의도를보다 표현 적으로 전달합니다.
나는이 질문이 얼마 전에 요청 된 것을 알고 있지만, 전에이 작업을 수행했으며 항목을 정렬 할 때 더 많은 제어를 할 수 있습니다. 웹 프로그래밍처럼 보이면 도움이됩니다. 당신 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>
중첩 된 레이아웃에서 패딩을 사용하여 원하는 방식으로 작동해야 할 수도 있습니다.
그것을위한 실제 솔루션 :
android:layout_weight="1"
TextView의 경우 버튼이 오른쪽으로 이동합니다!
이거 한번 해봐
<?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">
<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="0dp"
android:layout_weight="0.5"
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="0dp"
android:layout_weight="0.5"
android:layout_height="wrap_content"
android:background="@drawable/stitch_button"
android:layout_marginLeft="10dp"
android:text="@string/add"
android:layout_gravity="right"
android:layout_marginRight="15dp"/>
</LinearLayout>
이것은 당신의 문제를 해결할 것입니다
RelativeLayout을 사용하지 않거나 사용할 수없는 경우 방향을 "수직"및 너비를 "fill_parent"로하여 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" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<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>
LinearLayout의 방향이 수평이면 중력은 뷰에만 수직으로 영향을 미치기 때문입니다. 방향이 '수직'인 경우, 중력은 뷰에 수평으로 만 영향을줍니다. LinearLayout 방향 / 중력 설명에 대한 자세한 내용 은 여기 를 참조 하십시오 .
그냥 추가 안드로이드 : 중력 = "오른쪽" 이 라인의 부모는이 의지 작업을 레이아웃.
<LinearLayout
android:id="@+id/withdrawbuttonContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="@color/colorPrimary"
android:orientation="horizontal"
**android:gravity="right"**
android:weightSum="5"
android:visibility="visible">
<Button
android:id="@+id/bt_withDraw"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/BTN_ADD"
app:delay="1500" />
<Button
android:id="@+id/btn_orderdetail_amend"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/BTN_CANCEL"
app:delay="1500" />
</LinearLayout>
4 TextView
초 와 비슷한 레이아웃을 사용했습니다 . 2 개 TextView
는 왼쪽에 정렬하고 2 개 TextView
는 오른쪽에 정렬해야합니다. LinearLayouts
혼자서 사용하고 싶다면 여기 내 해결책이 있습니다 .
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:baselineAligned="false"
android:padding="10dp" >
<LinearLayout
android:layout_width="0dip"
android:layout_weight="0.50"
android:layout_height="match_parent"
android:gravity="left"
android:orientation="horizontal" >
<TextView
android:id="@+id/textview_fragment_mtfstatus_level"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/level"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/textview_fragment_mtfstatus_level_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
<LinearLayout
android:layout_width="0dip"
android:layout_weight="0.50"
android:layout_height="match_parent"
android:gravity="right"
android:orientation="horizontal" >
<TextView
android:id="@+id/textview_fragment_mtfstatus_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/time"
android:textAppearance="?android:attr/textAppearanceMedium"
/>
<TextView
android:id="@+id/textview_fragment_mtfstatus_time_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textAppearance="?android:attr/textAppearanceMedium"
/>
</LinearLayout>
</LinearLayout>
RelativeLayout을 사용하거나 Button의 부모 레이아웃에서 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="center_vertical|bottom|right|top"
android:layout_marginRight="15dp" />
중력이 아닌 layout_gravity에 유의하십시오.
기본 레이아웃으로 Linearlayout 대신 Relativelayout을 사용해야합니다. Relativelayout을 기본으로 사용하는 경우 상대 레이아웃이 alignParent 오른쪽, 왼쪽, 위쪽 및 아래쪽을 제공하므로 오른쪽의 버튼을 쉽게 처리하십시오.
android : layout_width = "75dp"와 같이 버튼에 레이아웃 너비를 사용하십시오.
참고 URL : https://stackoverflow.com/questions/10829835/right-align-button-in-horizontal-linearlayout
'Programming' 카테고리의 다른 글
두 날짜 사이의 월 차이 계산 (0) | 2020.07.16 |
---|---|
MySQL : LOAD DATA LOCAL INFILE 활성화 (0) | 2020.07.16 |
Android : 가로 모드 용 대체 레이아웃 XML (0) | 2020.07.16 |
ImageButton에 텍스트를 표시하는 방법? (0) | 2020.07.16 |
C #에서 사전을 JSON 문자열로 어떻게 변환합니까? (0) | 2020.07.16 |