gpt4 book ai didi

Android layout_weight 没有按预期工作

转载 作者:行者123 更新时间:2023-11-29 15:43:08 30 4
gpt4 key购买 nike

我正在尝试实现一个将分为 2 个子布局的布局。我希望左侧布局占屏幕的 1/4,而右侧布局占屏幕的剩余 3/4。我的目标如下:如果用户按下按钮,则左侧布局将被隐藏,右侧布局将占据整个屏幕(不确定这是否可行)。

为了实现这一点,我尝试使用以下布局,这似乎并不是预期的那样。

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="xeniasis.mymarket.MainActivity">

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:elevation="4dp"
android:padding="5dp"
android:theme="@style/ToolbarTheme" />

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

<LinearLayout
android:id="@+id/categoriesLayout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#000000"
android:orientation="vertical">

<!-- added a dummy button to see something -->
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>

<View
android:layout_width="2dp"
android:layout_height="fill_parent"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="@android:color/holo_blue_light" />

<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:background="#FFFFFF">

<!-- added a dummy button to see something -->
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
</LinearLayout>

如下图所示,问题在于左侧的 LinearLayout 仅与其内容一样宽,实际上并没有占据屏幕的 1/4。

enter image description here

最佳答案

你的问题是因为你将其父布局的宽度设置为wrap_content,如果你想要全屏宽度只需将它设置为match_parent,所以它会是这样的:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="xeniasis.mymarket.MainActivity">

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:elevation="4dp"
android:padding="5dp"
android:theme="@style/ToolbarTheme" />

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

.
.
.

关于Android layout_weight 没有按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37373448/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com