【问题标题】:Android Discrepancies between emulator and Eclipse SDK view layoutsAndroid 模拟器和 Eclipse SDK 视图布局之间的差异
【发布时间】:2011-10-26 01:21:36
【问题描述】:

我正在尝试显示一个带有一系列按钮的简单表格,以及它们右侧的标签(黑色文本)。

起初,SDK 图形布局中的一切看起来都是正确的。现在,(不做任何更改)SDK 中的一切看起来都是正确的,除了文本不是黑色的,如下所示:

当我在模拟器中运行它时,一切似乎都居中,并且文本是正确的颜色,但只显示了文本的最左侧。

这是怎么回事?

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent" android:orientation="vertical" android:background="@drawable/map_description_background">
    <TableLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/tableLayout1" android:layout_gravity="fill_horizontal">
        <TableRow android:id="@+id/tableRow1" android:layout_height="wrap_content" android:layout_marginTop="75px" android:layout_width="wrap_content">
            <ToggleButton android:textOff="Nautical" android:textOn="Statued" android:id="@+id/unitsButton" android:layout_width="wrap_content" android:layout_height="wrap_content"></ToggleButton>
            <TextView android:textAppearance="?android:attr/textAppearanceLarge" android:text="Units" android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/textView1" android:textColor="@color/maincolors"></TextView>
        </TableRow>
        <TableRow android:id="@+id/tableRow2" android:layout_width="wrap_content" android:layout_height="wrap_content">
            <ToggleButton android:textOff="GPS" android:textOn="Magnetic" android:id="@+id/toggleButton2" android:layout_width="wrap_content" android:layout_height="wrap_content"></ToggleButton>
            <TextView android:textAppearance="?android:attr/textAppearanceLarge" android:text="Heading" android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/textView3" android:textColor="@color/maincolors"></TextView>
        </TableRow>
        <TableRow android:id="@+id/tableRow3" android:layout_width="wrap_content" android:layout_height="wrap_content">
            <Button android:text="Help" android:onClick="clickHelpButton" android:id="@+id/helpButton" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
        </TableRow>
        <TableRow android:id="@+id/tableRow4" android:layout_width="wrap_content" android:layout_height="wrap_content"></TableRow>
    </TableLayout>
    <Button android:text="Done" android:id="@+id/done" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="clickDoneButton"></Button>

</LinearLayout>

【问题讨论】:

    标签: android android-layout


    【解决方案1】:

    Eclipse SDK 视图和模拟器并不总是相互一致;) 但模拟器通常与设备一致:)

    我不确定你的代码为什么会起作用,通常它与填充/边距有关,但也可能是 textapperancelarge 的东西(我自己没有使用它)。也不要使用 margin="75px" 真的不建议使用像 px 这样的硬编码值,而是使用 sp 。参考What is the difference between "px", "dp", "dip" and "sp" on Android?

    【讨论】:

    • 谢谢。我想我需要使用“px”。边距的原因是我有一个 9-patch PNG 背景图像,它有一个 ~75 像素的标题。由于标题部分没有缩放,我(相信我)最好以 px 指定边框。
    • 是的。那是 9-patch 背景图!经过进一步检查,它正在将所有东西(即我的布局)放入 9-patch 的中间/可拉伸区域!不知道这是怎么回事!你会认为“背景”应该像那样影响覆盖的内容!
    • 我以前也遇到过类似的问题,父母可能有 match_parent 作为高度和宽度,但它仍然紧紧地围绕着它的孩子。它就像 match_parent 告诉它可以拉伸,但如果孩子不需要它,它不会拉伸更多。
    • 我在这里找到了解决方法:stackoverflow.com/questions/3904852/…
    • 是有道理的,这类问题总是 = 边距/填充 =) 很高兴我能帮助找到问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-04
    • 1970-01-01
    • 2020-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-13
    相关资源
    最近更新 更多