【问题标题】:Android Layout: width half of parentAndroid布局:父级宽度的一半
【发布时间】:2013-11-15 11:56:39
【问题描述】:

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

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

有什么帮助吗?

【问题讨论】:

  • layout_weight 仅适用于orientation 指定的维度。也就是说,权重适用于垂直的高度和水平线性布局的宽度。

标签: android android-layout


【解决方案1】:

一种可能的方法是拥有一个主水平线性布局,将宽度拆分为 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>

【讨论】:

  • 谢谢,这对我有用。 Spacer 视图成功了。我会尽快接受答案。
【解决方案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】:

        简答:

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

        【讨论】:

        • 应该是公认的答案,但是,一个好的解释会让它变得更好。
        【解决方案6】:
        int params = linearLayout.getWidth()/2;
        
        ViewGroup.LayoutParams( params, ViewGroup.LayoutParams.WRAP_CONTENT));
        

        【讨论】:

        • 虽然这段代码 sn-p 可以解决问题,但它没有解释为什么或如何回答这个问题。请include an explanation for your code,因为这确实有助于提高您的帖子质量。请记住,您正在为将来的读者回答问题,而这些人可能不知道您的代码建议的原因。您可以使用edit 按钮改进此答案以获得更多选票和声誉!
        猜你喜欢
        • 2013-04-17
        • 2011-07-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-04-27
        • 1970-01-01
        相关资源
        最近更新 更多