Android 레이아웃 오른쪽 정렬
다음 레이아웃이 있습니다.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:layout_weight="1">
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/webview" android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
<LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="13">
<LinearLayout android:id="@+id/LinearLayout01" android:layout_width="wrap_content" android:layout_height="wrap_content">
<LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1">
<ImageButton android:background="@null" android:id="@+id/back" android:layout_width="fill_parent" android:layout_height="wrap_content" android:src="@drawable/back" android:padding="10dip" />
</LinearLayout>
<LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1">
<ImageButton android:background="@null" android:id="@+id/forward" android:layout_width="fill_parent" android:layout_height="wrap_content" android:src="@drawable/forward" android:padding="10dip" />
</LinearLayout>
</LinearLayout>
<RelativeLayout android:orientation="horizontal" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="1" >
<ImageButton android:background="@null" android:id="@+id/special" android:layout_width="fill_parent" android:layout_height="wrap_content" android:src="@drawable/barcode" android:padding="10dip" android:layout_gravity="right"/>
</RelativeLayout>
</LinearLayout>
</LinearLayout>
이 질문의 목적 상 저는 레이아웃의 아래쪽 절반에만 관심이 있습니다. 지금은 3 개의 이미지 버튼이 있습니다. 처음 2 개, 나란히 나란히 왼쪽 정렬하고 싶습니다. 세 번째는 오른쪽에 정렬하고 싶습니다.
현재 처음 2 개 버튼은 내가 원하는 위치에 있지만 세 번째 버튼은 왼쪽 정렬 상태를 유지합니다. 어떻게하면 바로 정렬 할 수 있습니까?
레이아웃은 매우 비효율적이고 부풀어 있습니다. 그렇게 많은 LinearLayout
s 가 필요하지 않습니다 . 사실 당신은 전혀 필요하지 않습니다 LinearLayout
.
하나만 사용하십시오 RelativeLayout
. 이렇게.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ImageButton android:background="@null"
android:id="@+id/back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/back"
android:padding="10dip"
android:layout_alignParentLeft="true"/>
<ImageButton android:background="@null"
android:id="@+id/forward"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/forward"
android:padding="10dip"
android:layout_toRightOf="@id/back"/>
<ImageButton android:background="@null"
android:id="@+id/special"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/barcode"
android:padding="10dip"
android:layout_alignParentRight="true"/>
</RelativeLayout>
하나만 사용하여 모든 작업을 수행 할 수 있습니다 RelativeLayout
(btw, android:orientation
매개 변수 가 필요하지 않음 ). 따라서 LinearLayout
많은 것을 포함하는 을 갖는 대신 다음과 같이 할 수 있습니다.
<RelativeLayout>
<ImageButton
android:layout_width="wrap_content"
android:id="@+id/the_first_one"
android:layout_alignParentLeft="true"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_toRightOf="@+id/the_first_one"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_alignParentRight="true"/>
</RelativeLayout>
아시다시피 일부 XML 매개 변수가 누락되었습니다. 입력해야하는 기본 매개 변수를 보여주고있었습니다. 나머지는 완료 할 수 있습니다.
LinearLayout 을 사용하려면 요소 layout_weight
와 정렬 할 수 있습니다 Space
.
예 배치 장소를 다음 textView
과 textView2
서로 옆에 textView3
오른쪽 정렬됩니다
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="@+id/textView" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="@+id/textView2" />
<Space
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="20dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="@+id/textView3" />
</LinearLayout>
당신은없이 동일한 효과를 얻을 수 있습니다 Space
사용자가 설정 한 것입니다 경우 layout_weight
에를 textView2
. 나는 더 분리 된 것을 좋아하고 Space
요소 를 보여주기 위해 더 좋아 합니다.
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="@+id/textView2" />
Note that you should (not must though) set layout_width
explicitly as it will be recalculated according to it's weight anyway (same way you should set height in elements of vertical LinearLayout
). For other layout performance tips see Android Layout Tricks series.
This is an example for a RelativeLayout:
RelativeLayout relativeLayout=(RelativeLayout)vi.findViewById(R.id.RelativeLayoutLeft);
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)relativeLayout.getLayoutParams();
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
relativeLayout.setLayoutParams(params);
With another kind of layout (example LinearLayout) you just simply has to change RelativeLayout for LinearLayout.
To support older version Space can be replaced with View as below. Add this view between after left most component and before right most component. This view with weight=1 will stretch and fill the space
<View
android:layout_width="0dp"
android:layout_height="20dp"
android:layout_weight="1" />
Complete sample code is given here. It has has 4 components. Two arrows will be on the right and left side. The Text and Spinner will be in the middle.
<ImageButton
android:id="@+id/btnGenesis"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center|center_vertical"
android:layout_marginBottom="2dp"
android:layout_marginLeft="0dp"
android:layout_marginTop="2dp"
android:background="@null"
android:gravity="left"
android:src="@drawable/prev" />
<View
android:layout_width="0dp"
android:layout_height="20dp"
android:layout_weight="1" />
<TextView
android:id="@+id/lblVerseHeading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:gravity="center"
android:textSize="25sp" />
<Spinner
android:id="@+id/spinnerVerses"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:gravity="center"
android:textSize="25sp" />
<View
android:layout_width="0dp"
android:layout_height="20dp"
android:layout_weight="1" />
<ImageButton
android:id="@+id/btnExodus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center|center_vertical"
android:layout_marginBottom="2dp"
android:layout_marginLeft="0dp"
android:layout_marginTop="2dp"
android:background="@null"
android:gravity="right"
android:src="@drawable/next" />
</LinearLayout>
참고URL : https://stackoverflow.com/questions/4305564/android-layout-right-align
'Development Tip' 카테고리의 다른 글
Django 사용자 정의 템플릿 태그의 액세스 요청 (0) | 2020.11.03 |
---|---|
Objective C의 iPhone 개발에서 "대리자"는 무엇입니까? (0) | 2020.11.03 |
자바 "?" (0) | 2020.11.03 |
SQL Server-부울 리터럴? (0) | 2020.11.03 |
Java 8의 기본 Xmxsize (0) | 2020.11.03 |