【发布时间】:2023-03-22 17:30:01
【问题描述】:
我试图将整个屏幕分成 3 个按钮,以便每个按钮占据屏幕的整个宽度,并且屏幕的长度在这些按钮之间平均分配。 xml文件的图形布局显示在http://i.stack.imgur.com/zSqVg.png
以下 XML 代码在我使用 ANDROID STUDIO 时有效。但是当我使用eclipse时它不起作用。它显示了 fragment_main.xml 的内容。(hello world!) 发生这种情况的原因可能是什么? 我发现当线性布局处于垂直方向时我无法使用权重,即使对于本示例中所示的最简单的情况也是如此。
这个布局的xml代码是-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="3" >
<Button
android:id="@+id/btnButton1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="button 1" />
<Button
android:id="@+id/btnButton2"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="button 2" />
<Button
android:id="@+id/btnButton3"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="button 3" />
</LinearLayout>
但是当我在 Eclipse 中运行代码时,我得到一个空屏幕。如果我删除 安卓:layout_weight="1" 从 xml 文件中的每个位置并将 android:layout_height 更改为 android:layout_height="40dp" (并删除 android:weightSum="3" )然后它会按照图形布局所示运行。
【问题讨论】:
-
尝试从布局声明中删除 weightSum,并将子级高度设置为 0dp,将权重设置为 1
-
发生了一些我们看不到的事情。您的布局按预期工作。您可以尝试清理您的项目。
-
感谢 codeMagic 和 zgc7009 调查我的问题!我删除了 weightSum 但问题仍然存在。清理项目也没有帮助。 @codeMagic 你介意分享你的 apk 文件吗?我的可以从这里下载:dropbox.com/s/cq58ocfjqlmns6u/Linear.apk
-
您的布局 xml 是正确的。尝试清理您的项目并检查您使用的布局是否与您正在修改的相同。
标签: android xml android-layout