我正在尝试从索尼爱立信手机克隆电源管理小部件,但我在使用新的小部件 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/