Android 布局:宽度为父级的一半

问题描述 投票:0回答:6

我有一个非常简单的布局,但无法使其看起来像我想要的那样。它是一个带有按钮和开关的 LinearLayout。我希望它们显示一个在另一个之上,但我希望它们的宽度是父布局的一半。

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

我一直在寻找其他类似的答案,但我找不到适合我的解决方案。这是我到目前为止所尝试过的:

<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
,但这会让他们消失。

请问有什么帮助吗?

android android-layout
6个回答
60
投票

一种可能的方法是拥有一个主水平 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>

31
投票

技巧是使用 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>

5
投票

简短回答:

  1. 在水平父布局中使用 android:weightSum="2"
  2. 在您的子布局中使用
  3. android:layout_weight="1"android:layout_width="0dp"

2
投票
试试这个代码

<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>
    

2
投票
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);

    


1
投票
int params = linearLayout.getWidth()/2; ViewGroup.LayoutParams( params, ViewGroup.LayoutParams.WRAP_CONTENT));
    
© www.soinside.com 2019 - 2024. All rights reserved.