【问题标题】:Strange layout behaviour - layout_weight and buttons奇怪的布局行为 - layout_weight 和按钮
【发布时间】:2019-05-24 18:56:06
【问题描述】:

我在我的 Android 应用程序中的布局有一个奇怪的问题(我正在学习它,所以我当然在制作一个计算器 :))。我已将所有按钮指定为行宽的 25%,但是当我在顶部添加 EditText 时,第一列变得比其他三列宽很多。 (截图来自 IntelliJ 预览,但是当我在我的设备上加载应用程序时,显示相同的结果。

当我删除 EditText 时,这个问题就消失了。我尝试在 XML 中的 EditText 中添加权重属性,但无济于事。

结果:

我的代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent">
    <TableLayout android:layout_height="wrap_content"
                 android:layout_width="fill_parent"
                 android:weightSum="1.0">
        <TableRow android:layout_width="fill_parent"
                  android:weightSum="1.0">
            <EditText android:layout_weight="1.0"
                      android:text="Edit me"></EditText>
        </TableRow>
        <TableRow android:layout_width="fill_parent"
                  android:weightSum="1.0">
            <Button android:text="7"
                    android:layout_weight="0.25"></Button>
            <Button android:text="8"
                    android:layout_weight="0.25"></Button>
            <Button android:text="9"
                    android:layout_weight="0.25"></Button>
            <Button android:text="/"
                    android:layout_weight="0.25"></Button>
        </TableRow>
        <TableRow android:layout_width="fill_parent"
                  android:weightSum="1.0">
            <Button android:text="4"
                    android:layout_weight="0.25"></Button>
            <Button android:text="5"
                    android:layout_weight="0.25"></Button>
            <Button android:text="6"
                    android:layout_weight="0.25"></Button>
            <Button android:text="*"
                    android:layout_weight="0.25"></Button>
        </TableRow>
        <TableRow android:layout_width="fill_parent"
                  android:weightSum="1.0">
            <Button android:text="1"
                    android:layout_weight="0.25"></Button>
            <Button android:text="2"
                    android:layout_weight="0.25"></Button>
            <Button android:text="3"
                    android:layout_weight="0.25"></Button>
            <Button android:text="-"
                    android:layout_weight="0.25"></Button>
        </TableRow>
        <TableRow android:layout_width="fill_parent">
            <Button android:text="0"
                    android:layout_weight="0.25"></Button>
            <Button android:text="."
                    android:layout_weight="0.25"></Button>
            <Button android:text="+/-"
                    android:layout_weight="0.25"></Button>
            <Button android:text="="
                    android:layout_weight="0.25"></Button>
        </TableRow>
    </TableLayout>
</LinearLayout>

【问题讨论】:

    标签: android android-layout width android-layout-weight


    【解决方案1】:

    如果您的编辑文本填满了屏幕的整个宽度,为什么不把它放在您的表格之外呢?像这样?

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  android:layout_width="fill_parent"
                  android:layout_height="fill_parent"
                  android:orientation="vertical">
        <EditText android:layout_width="fill_parent"
                  android:layout_height="wrap_content"
                          android:text="Edit me"/>
        <TableLayout android:layout_height="wrap_content"
                     android:layout_width="fill_parent"
                     android:weightSum="1.0">
            <TableRow android:layout_width="fill_parent"
                      android:weightSum="1.0">
                ...
    

    【讨论】:

    • 黄金!我忘记了方向,这就是为什么我没有注意到这种方式。谢谢。
    猜你喜欢
    • 2014-02-25
    • 2018-04-19
    • 2016-10-22
    • 1970-01-01
    • 1970-01-01
    • 2012-08-03
    • 1970-01-01
    • 1970-01-01
    • 2015-09-08
    相关资源
    最近更新 更多