development

Android : 사용 가능한 공간을 채우기 위해 뷰를 확장하는 방법은 무엇입니까?

big-blog 2020. 9. 23. 07:51
반응형

Android : 사용 가능한 공간을 채우기 위해 뷰를 확장하는 방법은 무엇입니까?


이것은 간단 해 보이지만 어떻게해야할지 모르겠습니다. EditText와 두 개의 ImageButton이있는 가로 레이아웃이 있습니다. ImageButton은 고정 된 크기이고 EditText는 레이아웃의 나머지 공간을 차지하기를 원합니다. 이것이 어떻게 성취 될 수 있습니까?

<LinearLayout 
        android:orientation="horizontal"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content">
        <EditText 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
        </EditText>
        <ImageButton 
            android:src="@drawable/img1"
            android:layout_width="50dip" 
            android:layout_height="50dip">
        </ImageButton>
        <ImageButton 
            android:src="@drawable/img2"
            android:layout_width="50dip" 
            android:layout_height="50dip">
        </ImageButton>
</LinearLayout>

상대 레이아웃에서 시도

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content">
    <ImageButton 
        android:id="@+id/btn1"
        android:src="@drawable/icon"
        android:layout_width="50dip" 
        android:layout_height="50dip"
        android:layout_alignParentRight="true"/>
    <ImageButton 
        android:id="@+id/btn2"
        android:src="@drawable/icon"
        android:layout_width="50dip" 
        android:layout_height="50dip"
        android:layout_toLeftOf="@+id/btn1"/>
    <EditText 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@+id/btn2">
    </EditText>

</RelativeLayout>

레이아웃을 동일하게 유지하려면 (예 : 텍스트 뒤의 버튼) layout_weight속성과 함께을 사용하십시오 fill_parent.

<LinearLayout 
    android:orientation="horizontal"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content">
    <EditText 
        android:layout_weight="1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
    </EditText>
    <ImageButton 
        android:src="@drawable/img1"
        android:layout_width="50dip" 
        android:layout_height="50dip">
    </ImageButton>
    <ImageButton 
        android:src="@drawable/img2"
        android:layout_width="50dip" 
        android:layout_height="50dip">
    </ImageButton>
 </LinearLayout>

주는 일 EditText'무게 것은'는 지난 파악하고, 버튼에 우선 순위를 제공받을 수 있습니다. 다른 layout_weights로 플레이하고 얼마나 재미있을 수 있는지 확인하십시오!


괜찮아:

<RelativeLayout 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content">

        <ImageButton 
            android:id="@+id/button1"
            android:src="@drawable/img1"
            android:layout_width="50dip"
            android:layout_alignParentTop="true" 
            android:layout_height="50dip">
        </ImageButton>
        <ImageButton 
            android:src="@drawable/img2"
            android:layout_width="50dip" 
            android:layout_alignParentTop="true"
            android:layout_toRightOf="@id/button1"
            android:layout_height="50dip">
        </ImageButton>
        <EditText 
            android:layout_below="@id/button"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent">
        </EditText>
</RelativeLayout>

레이아웃의 요점은 다음과 같습니다.

  • The items with fixed sized are placed first in the layout, this way when Android comes to calculating the height of the height of things with fill_parent later in the file it only takes into account the remaining space, not all of the space it could possibly occupy if nothing else was there
  • The EditText has a height of fill_parent (match_parent in 2.2+) so that it takes up the rest of the available space

Sorry didn't read your question will enough. The above code provides the answer if you want to do it in a vertical fashion. For a horizontal layout the code posted by @Sunil should work.


Use set the layout_width or layout_height to 0dp (By the orientation you want to fill remaining space). Then use the layout_weight to make it fill remaining space.

참고URL : https://stackoverflow.com/questions/5406092/android-how-to-make-a-view-grow-to-fill-available-space

반응형