安卓版面: width half of parent

标签 android android-layout

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

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

这样,按钮和开关占用了父级的所有空间,而不是只占用一半。 我已经尝试在 child 中使用 android:layout_width = 0dp ,但这会使它们消失。

有什么帮助吗?

最佳答案

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

关于安卓版面: width half of parent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20000560/

相关文章:

android - 如何在Kotlin android中的扩展函数中访问同名的类成员

android - 如何在Imageview中获取图像的高度?

Android实时游戏——实现时间单位

java - Firebase简单聊天客户端无法检索值

PHP-MySQL | PHP-MySQL那么更好/更干净的方式吗?

java - 全局布局监听器返回 0

android - 如果 Activity 布局更改,配置更改后恢复 Fragment 状态

android - 根据屏幕宽度制作 View 的宽度

android - 自己定义的 Layout , onDraw() 方法没有被调用

android - 验证 C2DM Auth Token - Android/Urban Airship