【问题标题】:Linear Layout too big for widget size线性布局对于小部件尺寸来说太大了
【发布时间】:2011-02-03 01:23:59
【问题描述】:

我正在尝试制作一个 4x1 的小部件。 我将提供者设置如下

<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="294dp"
android:minHeight="72dp"
android:updatePeriodMillis="0"
android:initialLayout="@layout/widgetlayout"></appwidget-provider>

小部件布局如下

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="320dp" 
android:layout_height="100dp"
android:background="@drawable/widgetframe">
<Button android:id="@+id/butscan"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click Me" />
<Button android:id="@+id/butenable"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="On/Off"
        android:paddingRight="5dip"/>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:paddingLeft="4dip"
    android:includeFontPadding="false"
    android:id="@+id/lblcurrent"
    android:text="Test"/></LinearLayout>

现在当我在模拟器上尝试这个时,我只得到主屏幕上显示的图层的中间部分。背景图片是来自 SDK (320x100) 的 4x1 肖像图片。

我错过了什么?

【问题讨论】:

    标签: android android-widget android-layout


    【解决方案1】:

    LinearLayout 的布局宽度和高度应该是 fill_parent。

    您的按钮没有任何图形资源,但我假设您在代码中分配了诸如可绘制对象或位图之类的东西,否则它们可能不会显示。我从来没有创建过没有图像的按钮,所以我不知道它会如何作用。

    我很惊讶您的“测试”文本没有显示,但它可能显示在您的小部件尺寸之外,因为您的背景比小部件本身大。再次尝试使用 fill_parent 而不是 320dp 和 100dp(顺便说一句,我从未使用过 'dp' 只使用过 'sp')。

    【讨论】:

      【解决方案2】:

      由于fill_parent,您的 TextView 正在接管布局。

      您可能应该将layout_weight=1 添加到它,因此它只会扩展到可用空间。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-07-06
        • 2013-06-07
        • 2012-07-22
        • 1970-01-01
        • 2019-04-07
        • 1970-01-01
        • 1970-01-01
        • 2012-04-11
        相关资源
        最近更新 更多