Android 小部件填充问题

标签 android widget margin padding android-4.0-ice-cream-sandwich

我正在尝试从索尼爱立信手机克隆电源管理小部件,但我在使用新的小部件 ICS 填充时遇到了问题。

我已阅读这篇文章 [下面的链接],但我仍然遇到问题。

我在 GB 上运行了我的小部件,它就像我想要的那样工作(第二个是克隆):
http://i.minus.com/iQmbhue0z0jJz.png

但是在 ICS 中运行时,我的小部件变得比原来的小:
http://i.minus.com/iudmWwv6xBsxA.png

我的文件是:


widget_info.xml (@xml/widget_info.xml)

<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
    android:minWidth="40.0dip"
    android:minHeight="40.0dip"
    android:updatePeriodMillis="0"
    android:initialLayout="@layout/widget" >
</appwidget-provider>

widget.xml (@layout/widget.xml)

<ImageView android:id="@+id/widget1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:contentDescription="@string/app_name"
        android:src="@drawable/sms_disabled"
        android:clickable="true"
        xmlns:android="http://schemas.android.com/apk/res/android" />

我已经尝试将 android:padding="0dp" 放在 ImageView 中,我已经尝试将布局放在 widget.xml 上,然后将 android :padding="0dp" 就可以了,但我仍然遇到这个问题。

有什么建议吗?

最佳答案

小部件在 ICS 上较小,因为 Android 4.0 或更高版本会自动在小部件周围创建边距(另请参见 Android developers API)。解决方案是创建两个 xml 文件;一种用于 ICS 或更高版本,没有额外边距,一种用于较旧的 Android 版本,有额外边距。

res/values/dimens.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <dimen name="widget_margin">8dp</dimen>
</resources>

res/values-v14/dimens.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <dimen name="widget_margin">0dp</dimen>  
</resources>

然后将 android:layout_margin="@dimen/widget_margin" 添加到您的 widget xml 文件中的布局

但是,带有(某些版本的?)Samsungs Touchwiz Launcher 的手机不会在 ICS 上应用自动边距,因此您可能还想对此进行补偿。有关此问题的更多信息:Widget margins not applied on ICS phone with TouchWiz launcher?

关于Android 小部件填充问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11877421/

相关文章:

java - 我的应用程序的调试构建工作正常但不是发布构建签名版本

animation - 补间动画和动画 Controller 的区别

javascript - 如何仅将边距添加到 <td> 元素的第一行

css - 为什么 <p> 和 <hr> 之间的垂直边距在 IE7 中不会折叠

android - Android中如何使TextView中的文本结尾透明

android - 在没有 SurfaceView 的情况下使用 Exoplayer

java - onclick 取决于 View 所在的位置

python - 如何检测 OptionMenu 或 Checkbutton 何时更改?

python-3.x - 如何在 Bokeh 中为动画实现开始停止按钮/播放恢复按钮?

css - 如何在 CSS 中根据高度(默认情况下根据宽度)设置边距/填充百分比 ..?