답변:
woodshy의 답변이 저에게 효과적이며 Ungureanu Liviu의 답변보다 사용하지 않기 때문에 간단합니다 RelativeLayout. 명확성을 위해 레이아웃을 제공하고 있습니다.
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<Button
android:layout_width = "80dp"
android:layout_weight = "0"
android:layout_height = "wrap_content"
android:text="<"/>
<TextView
android:layout_width = "fill_parent"
android:layout_height = "wrap_content"
android:layout_weight = "1"/>
<Button
android:layout_width = "80dp"
android:layout_weight = "0"
android:layout_height = "wrap_content"
android:text=">"/>
</LinearLayout>
alignLeft및 alignParentLeft달성 될 수 없다 등,, LinearLayout.
<TEXT VIEW>가 LinearLayout에 배치 된 경우 TextView에 대해 Layout_weight 속성을 <및>의 0 및 1로 설정하십시오.
RelativeLayout의 경우 <및>를 왼쪽과 오른쪽으로 정렬하고 TextView의 "Layout to left of"및 "Layout to right of"속성을 <및>의 id로 설정하십시오.
를 사용 RelativeLayout하면 다음과 같이 할 수 있습니다.
<RelativeLayout
android:layout_width = "fill_parent"
android:layout_height = "fill_parent">
<ImageView
android:id = "@+id/my_image"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:layout_alignParentTop ="true" />
<RelativeLayout
android:id="@+id/layout_bottom"
android:layout_width="fill_parent"
android:layout_height = "50dp"
android:layout_alignParentBottom = "true">
<Button
android:id = "@+id/but_left"
android:layout_width = "80dp"
android:layout_height = "wrap_content"
android:text="<"
android:layout_alignParentLeft = "true"/>
<TextView
android:layout_width = "fill_parent"
android:layout_height = "wrap_content"
android:layout_toLeftOf = "@+id/but_right"
android:layout_toRightOf = "@id/but_left" />
<Button
android:id = "@id/but_right"
android:layout_width = "80dp"
android:layout_height = "wrap_content"
android:text=">"
android:layout_alignParentRight = "true"/>
</RelativeLayout>
</RelativeLayout>
>버튼이 아닌 모든 공간을 채우지 않는 이유는 무엇 입니까?
를 사용하여 ConstraintLayout다음과 같은 것을 발견했습니다.
<Button
android:id="@+id/left_button"
android:layout_width="80dp"
android:layout_height="48dp"
android:text="<"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toRightOf="@+id/left_button"
app:layout_constraintRight_toLeftOf="@+id/right_button"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/right_button"
android:layout_width="80dp"
android:layout_height="48dp"
android:text=">"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
공장. 핵심은 오른쪽, 왼쪽, 위쪽 및 아래쪽 가장자리 제약 조건을 적절하게 설정 한 다음 너비와 높이를 설정하고 0dp자체 크기를 알아내는 것입니다.
TextView왼쪽 및 오른쪽 제약 조건이 있는지 확인하십시오 . 이 TextView(가) 첫 번째 경우 스트레칭하지 않는 Button받는 권리 제약 조건이 TextView마지막이 Button받는 왼쪽 제약이있다 TextView.
간단합니다 minWidth 또는 minHeight를 설정하십시오. 가로 또는 세로로 찾고있는 대상에 따라 다릅니다. 나머지 공간을 채우려는 다른 객체의 경우 가중치를 1로 설정하고 (내용을 감싸는 너비를 설정) 나머지 영역을 채 웁니다.
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center|left"
android:orientation="vertical" >
</LinearLayout>
<LinearLayout
android:layout_width="80dp"
android:layout_height="fill_parent"
android:minWidth="80dp" >
</LinearLayout>
높은 layout_weight 속성을 사용할 수 있습니다. 아래에서 ListView는 아래쪽에 버튼이있는 모든 여유 공간을 차지하는 레이아웃을 볼 수 있습니다
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context=".ConfigurationActivity"
android:orientation="vertical"
>
<ListView
android:id="@+id/listView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1000"
/>
<Button
android:id="@+id/btnCreateNewRule"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Create New Rule" />
<Button
android:id="@+id/btnConfigureOk"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Ok" />
</LinearLayout>
<LinearLayout...>내가했던 것과 같은 결함을 가진 사람들을 위해 :
지정하는 것이 중요하며 android:layout_width="fill_parent"작동하지 않습니다 wrap_content.
OTOH, 당신은 생략 할 수 있습니다 android:layout_weight = "0", 그것은 필요하지 않습니다.
내 코드는 기본적으로 https://stackoverflow.com/a/25781167/755804 의 코드와 동일합니다 (Vivek Pandey 작성)
상대 레이아웃은 항상 드로잉에 2 패스 (다른 유형의 레이아웃의 경우 1)를 만들기 때문에 2 상대 레이아웃 중첩을 피해야합니다. 중첩하면 지수가됩니다. 왼쪽 공간을 채우려는 요소에 width = 0 및 weight = 1 인 선형 레이아웃을 사용해야합니다. 이 답변은 성능과 관행에 더 좋습니다. 기억하십시오 : 다른 선택이 없을 때만 상대 레이아웃을 사용하십시오.
<?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="match_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/imageview"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal">
<Button
android:id="@+id/prev_button"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:text="<" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ellipsize="end"
android:singleLine="true"
android:gravity="center"
android:text="TextView" />
<Button
android:id="@+id/next_button"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:text=">" />
</LinearLayout>
</LinearLayout>
Relativelayout을 사용하여 LinearLayout을 래핑하십시오.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:round="http://schemas.android.com/apk/res-auto"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:text="<"/>
<TextView
android:layout_width = "fill_parent"
android:layout_height = "wrap_content"
android:layout_weight = "1"/>
<Button
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:text=">"/>
</LinearLayout>
</RelativeLayout>`
상대 레이아웃을 사용하는 경우 확장해야 할 두보기에 고정하여보기를 늘릴 수 있습니다. 지정된 높이는 무시되지만 Android에는 여전히 높이 속성이 필요하므로 "0dp"를 썼습니다. 예:
<View
android:id="@+id/topView"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_alignParentTop="true"
android:layout_marginTop="8dp"/>
<View
android:id="@+id/stretchableView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_below="@id/topView"
android:layout_above="@+id/bottomView"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:adjustViewBounds="true"/>
<View
android:id="@id/bottomView"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_alignParentBottom="true"
android:layout_marginBottom="16dp"/>