Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android布局:宽度为父级的一半_Android_Android Layout - Fatal编程技术网

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

Android布局:宽度为父级的一半,android,android-layout,Android,Android Layout,我有一个非常简单的布局,我不能使它看起来像我想要的。这是一个带有按钮和开关的线性布局。我希望它们显示一个在另一个之上,但我希望它们的宽度是父布局的一半 |--LinearLayout---------| | | | ----------- | || Switch | | | ----------- | | ----------- | || button |

我有一个非常简单的布局,我不能使它看起来像我想要的。这是一个带有按钮和开关的线性布局。我希望它们显示一个在另一个之上,但我希望它们的宽度是父布局的一半

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


有什么帮助吗?

诀窍是使用0dp通过layout\u-weight管理您想要的尺寸! 试试这个


试试这段代码

<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,并在其内部使用垂直布局

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

将此
buttonWidth
设置为类似于
button.setWidth(buttonWidth)的按钮

简短回答:

  • 在水平父布局中使用android:weightSum=“2”
  • 在您的子布局中使用android:layout\u weight=“1”android:layout\u width=“0dp”

  • layout\u weight
    仅应用于用
    方向指定的尺寸。也就是说,重量适用于垂直布局的高度和水平线性布局的宽度。谢谢,这是我的工作。这种观点起了作用。我会尽快接受答案。虽然这个代码片段可以解决这个问题,但它没有解释为什么或者如何回答这个问题。请,因为这确实有助于提高你的文章质量。请记住,您将在将来回答读者的问题,这些人可能不知道您的代码建议的原因。您可以使用该按钮改进此答案,以获得更多选票和声誉!
    
        <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>
    
    Button button = (Button) findViewById(R.id.share_button);
    DisplayMetrics displaymetrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
    int width = displaymetrics.widthPixels;
    int buttonWidth = width/2;
    
    int params = linearLayout.getWidth()/2;
    
    ViewGroup.LayoutParams( params, ViewGroup.LayoutParams.WRAP_CONTENT));