[android] Android 레이아웃 가중치

저는 Android 개발이 처음이고 선형 레이아웃에서 가중치를 설정하는 것에 대한 질문이 있습니다.

두 개의 사용자 지정 단추와 사용자 지정 편집 텍스트가있는 행을 만들려고합니다. 편집 텍스트는 내용만큼만 공간을 차지해야하며 두 개의 버튼은 행에서 사용 가능한 나머지 공간을 채우기 위해 가로로 확장되어야합니다. 이렇게 …

| [#Button++#] [#Button--#] [et] |

여러 번 시도한 후에는 지나치게 복잡해 보이지만 원하는 것을 얻을 수있는 가장 가까운 방법입니다.

| [Button] [Button] [###ET###] |

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal|center_vertical"
    android:layout_weight="1"
    >

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center_horizontal|center_vertical"
        >
        <RelativeLayout
            android:id="@+id/btn_plus_container"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            >
            <ImageButton
                android:layout_height="wrap_content"
                android:layout_width="fill_parent"
                android:id="@+id/btn_plus"
                android:background="@drawable/btn"
                android:layout_centerInParent="true"
                ></ImageButton>
            <TextView
                android:textColor="#FAFAF4"
                android:id="@+id/tv_dice_plus"
                android:text="@string/tv_plus"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                ></TextView>
        </RelativeLayout>
    </LinearLayout>
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center_horizontal|center_vertical"
        >
        <RelativeLayout
            android:id="@+id/btn_minus_container"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            >
            <ImageButton
                android:layout_height="wrap_content"
                android:layout_width="fill_parent"
                android:id="@+id/btn_minus"
                android:background="@drawable/btn"
                android:layout_centerInParent="true"
                ></ImageButton>
            <TextView
                android:textColor="#FAFAF4"
                android:id="@+id/btn_minus"
                android:text="@string/tv_minus"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                ></TextView>
        </RelativeLayout>
    </LinearLayout>
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center_horizontal|center_vertical"
        >
        <EditText
            android:textColor="#FAFAF4"
            android:text="@string/et_output"
            android:id="@+id/et_output"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:inputType="number"
            android:selectAllOnFocus="true"
            android:minWidth="50sp"
            android:maxWidth="50sp"
            android:background="@drawable/tv_black_background3"
            android:textColorHighlight="#c30505"
            ></EditText>
    </LinearLayout>
</LinearLayout>

내가 이해했듯이 버튼을 잡고있는 선형 레이아웃에 대한 [android : layout_weight = “2”] 설정은 편집 텍스트보다 더 많은 공간을 차지해야하지만 저에게는 반대의 역할을하므로 둘 다 크기가 절반으로 줄어 듭니다.

| [Btn] [Btn] [#####et#####] |

나는 너무 많은 다른 조합을 시도하여 기억조차 할 수 없으며 아무도 작동하지 않았습니다.



답변

너비로 fill_parent를 사용하고 있기 때문에 작동하지 않습니다. 가중치는 남은 빈 공간을 분배 하거나 총 합계가 LinearLayout보다 클 때 공간을 제거 하는 데 사용됩니다 . 대신 너비를 0dip으로 설정 하면 작동합니다.


답변

android:Layout_weight이미 너비에 고정 값을 붙이지 않았을 때 사용할 수 있습니다 fill_parent.

다음과 같이하십시오.

<Button>

Android:layout_width="0dp"
Android:layout_weight=1

-----other parameters
</Button>


답변

그렇게 생각하면 더 간단해질 것입니다

버튼이 3 개 있고 그에 따라 가중치가 1,3,1이면 HTML의 테이블처럼 작동합니다.

해당 라인에 5 개 부분 제공 : 버튼 1에 1 개, 버튼 2에 3 개, 버튼 1에 1 개 부분


답변

linearLayout에서 WeightSum = 2를 설정합니다.

그리고 당신이 표시하기를 원하는대로 아이에게 가중치를 분배합니다. 나는 아이에게 가중치 = “1”을주었습니다. 그래서 둘 다 전체의 절반을 분배 할 것입니다.

 <LinearLayout
    android:id="@+id/linear1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:weightSum="2"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/ring_oss"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:src="@drawable/ring_oss" />

    <ImageView
        android:id="@+id/maila_oss"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:src="@drawable/maila_oss" />
</LinearLayout>


답변

가중치 값 0-1은 가중치 값에 비례하여 사용 가능한 공간의 분포를 공유합니다 (layout_width = “0px”설정 후). 가중치가 지정되지 않은 뷰 요소 (가중치 항목 없음)는 가중치 0을 가져와 확장이 없음을 의미합니다.

가중치 항목이 필요없는 간단한 대안은 텍스트 (wrap_content)에 필요한 최소값에서 사용 가능한 공간으로 확장하도록 지시하는 텍스트가있는보기에 마키를 첨부하는 것입니다. EditText : android : layout_width = “wrap_content”android : ellipsize = “marquee”


답변

나는 EditTexts 너비를 로 설정하고 wrap_content두 개의 버튼을 LinearLayout너비가 fill_parent1로 설정된 a에 넣는다 고 가정합니다 .


답변

내가 찾은 또 하나의 이유 (들리는대로 모호함). 아래는 작동하지 않았습니다.

LinearLayout 수직

LinearLayout 높이 fillparent + 가중치

LinearLayout 높이 fillparent + 가중치

LinearLayout 높이 fillparent + 가중치

EndLinearLayout

무슨 일이 있었는지

RelativeLayout

LinearLayout 수직

LinearLayout 높이 fillparent + 가중치

LinearLayout 높이 fillparent + 가중치

LinearLayout 높이 fillparent + 가중치

EndLinearLayout

EndRelativeLayout

선형을 사용하는 루트 레이아웃에서 모호하게 들리며 그 아래의 가중치가 작동하지 않았습니다. 그리고 내가 “작동하지 않았다”라고 말하면 다양한 해상도 사이의 그래픽 레이아웃을 본 후 화면 일관성이 크게 깨 졌다는 의미입니다.