【问题标题】:Android button stretched in 4.1 but not in 2.2Android 按钮在 4.1 中拉伸,但在 2.2 中没有
【发布时间】:2012-09-19 20:19:28
【问题描述】:

我有一个简单的待办事项列表应用程序,带有一个黑色加号形状的按钮,可以将事情添加到列表中。在 android 2.2 中,黑色加号如下所示:

但是,在 android 4.1 中,黑色加号看起来像这样:

加号应该看起来像它在 2.2 中的样子。这是我的相应片段的 xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:id="@+id/widget78"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:focusable="true" android:focusableInTouchMode="true">
<ListView
    android:id="@+id/taskslist"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="10" />
<LinearLayout
    android:id="@+id/widget83"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="12dp"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp">
<EditText
    android:id="@+id/taskname"
    android:layout_width="wrap_content" 
    android:layout_height="fill_parent"
    android:background="@drawable/edittextbackground"
    android:layout_marginBottom="0dp"
    android:layout_marginLeft="0dp"
    android:layout_marginRight="2dp"
    android:hint=" Add Something :)"
    android:textSize="18sp"
    android:windowSoftInputMode="adjustNothing"
    android:cursorVisible="true"
    android:textColor="#000000"
    android:textCursorDrawable="@null"
    android:layout_weight="9999999"/>
<!--    android:imeOptions="actionGo"
    android:inputType="textImeMultiLine" 
    --> 
<FrameLayout
    android:layout_height="wrap_content"
    android:layout_width="wrap_content">
<Button
    android:id="@+id/addtask"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/small_black_plus"
    android:layout_marginBottom="0dp"
    android:layout_gravity="center"
    android:layout_marginLeft="2dp"
    android:layout_marginRight="0dp"
    android:text="" />
</FrameLayout>
</LinearLayout>
</LinearLayout>

为什么两个版本的布局看起来不同?我怎样才能让它们看起来一样?

【问题讨论】:

    标签: android android-layout android-version


    【解决方案1】:

    我认为这可能与您管理资源文件的方式有关。您测试的设备是否具有不同的分辨率?您看到的失真可能是由于操作系统自动缩放您放入 drawable-mdpi 或 drawable-hdpi 文件夹中的资源以用于 xhdpi 屏幕。

    这里是如何管理资源文件的很好参考,希望对您有所帮助:

    http://developer.android.com/guide/practices/screens_support.html

    编辑:

    另外,看看这个问题:

    Difference: android:background and android:src?

    正如其他人所说,您可以尝试将您的 Button 更改为 ImageView。 android 中的任何视图都可以通过设置 onClickListener 来用作按钮,就像使用 Button 一样。如果您这样做,请尝试使用android:src 属性而不是android:background 设置您的图像可绘制资源。我不认为这是您的问题的原因,因为我不确定不同版本的 Android 会导致您的视图大小不同,但也许在这两个想法之间,您可以找出一些可行的方法。

    【讨论】:

    • 我已经在带有 ICS 的 Galaxy Nexus、带有 Gingerbread 的 LG Lucid 和两个模拟器上进行了测试。我发布的屏幕截图来自模拟器。但是,2.2 模拟器图片看起来和 lucid 上的一样,而 4.1 模拟器看起来和 nexus 上的一样。模拟器都说“hw.lcd.density: 240”,但我不知道这是什么意思。显然,nexus 的屏幕比 lucid 大得多,操作栏上还有一个设置按钮。
    • 您对我错误地管理资源文件的看法可能是正确的。我只是为所有屏幕尺寸使用一张图像。从那以后,我创建了 4 张图像,每种屏幕尺寸一张。尽管如此,android 4 版本仍然显得捉襟见肘。我会更仔细地阅读您的材料并尝试修复它。
    【解决方案2】:

    这是因为 4.1 中的新 TabHost 允许“可滚动”标签。看看如何水平滚动它?

    为了使这在两个版本的 Android 上看起来都正确,我将使用资源限定符(在文件夹名称中)为不同的 API 级别创建一个 ViewStub,并为不同的选项卡使用相同的 ID,但明确设置您的字体不适合的尺寸(或更改文本,这取决于您)

    【讨论】:

      【解决方案3】:

      或者您可以使用 ImageView 而不是 Button,它们在您的情况下的工作方式相同。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-05-26
        • 2021-05-20
        • 1970-01-01
        • 1970-01-01
        • 2018-07-17
        • 2016-05-12
        相关资源
        最近更新 更多