Подтвердить что ты не робот

Платформа Android: ширина половины родительского

У меня очень простой макет, который я не могу сделать так, как будто хочу. Это LinearLayout с кнопкой и переключателем. Я хочу, чтобы они показывали один над другим, но я хочу, чтобы их ширина была половиной родительского макета.

|--LinearLayout---------|
|                       |
| -----------           |
||   Switch  |          |
| -----------           |
| -----------           |
||   button  |          |
| -----------           |
 ------------------------

Я смотрел на другой подобный ответ в SO, но я не мог найти решение, которое работает для меня. Это то, что я пробовал до сих пор:

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:weightSum="2" >

                <Switch
                    android:id="@+id/remember_me_switch"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:hint="@string/remember" />

                <Button
                    android:id="@+id/share_button"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:onClick="loginOnclick"
                    android:text="@string/login_button_text" />

            </LinearLayout>

При этом кнопка и коммутатор занимают все пространство родителя вместо того, чтобы брать только половину. Я пробовал с android:layout_width = 0dp у детей, но он заставляет их исчезать.

Любая помощь, пожалуйста?

4b9b3361

Ответ 1

Один из возможных способов состоит в том, чтобы иметь горизонтальный LinearLayout, который разделяет ширину на 2, и внутри нее имеет вертикальную компоновку

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >
        <LinearLayout
            android:layout_height="wrap_content"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:orientation="vertical" >

            <Switch
                android:id="@+id/remember_me_switch"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="@string/remember" />

            <Button
                android:id="@+id/share_button"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:onClick="loginOnclick"
                android:text="@string/login_button_text" />

        </LinearLayout>
        <!-- Right side spacer -->
        <View
           android:layout_width="0dp"
           android:layout_height="1dp"
           android:layout_weight="1" />

    </LinearLayout>

Ответ 2

Трюк заключается в использовании 0dp для размера, который вы хотите управлять, с помощью layout_weight! Попробуйте это

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="2" >

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:layout_weight="1"
            android:weightSum="2" >

            <Switch
                android:id="@+id/remember_me_switch"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:hint="@string/remember" />

            <Button
                android:id="@+id/share_button"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:onClick="loginOnclick"
                android:text="@string/login_button_text" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:layout_weight="1" >
        </LinearLayout>

    </LinearLayout>

Ответ 3

Попробуйте этот код

<LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:weightSum="2" >

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

                <Switch
                    android:id="@+id/remember_me_switch"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:hint="@string/remember" />

               <View 
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"/>
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

                <Button
                    android:id="@+id/share_button"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:onClick="loginOnclick"
                    android:text="@string/login_button_text" />

               <View 
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"/>
             </LinearLayout>
        </LinearLayout>

Ответ 4

Button button = (Button) findViewById(R.id.share_button);
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int width = displaymetrics.widthPixels;
int buttonWidth = width/2;

установите эту buttonWidth на вашу кнопку, например button.setWidth(buttonWidth);

Ответ 5

int params = linearLayout.getWidth()/2;

ViewGroup.LayoutParams( params, ViewGroup.LayoutParams.WRAP_CONTENT));