Android 小部件 WRAP_CONTENT 不工作
Android widget WRAP_CONTENT not working
我的应用程序中有以下小部件,它包含一个 ImageView
,宽度和高度设置为 WRAP_CONTENT
。根视图再次是 RelativeLayout
,宽度和高度设置为 WRAP_CONTENT
。
我的问题是为什么小部件的顶部和底部有一些填充(标记为黑色)?我希望它是完美的正方形。以下是我的小部件布局。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/widget_battery_2x2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#000000"
android:layout_margin="@dimen/widget_margin"
android:padding="@dimen/widget_padding" >
<ImageView
android:id="@+id/image_widget_arc_battery"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ff0000"
android:layout_alignParentLeft="true"/>
</RelativeLayout>
@dimen/widget_margin
且 @dimen/widget_padding
为零。
ImageView
包含一个动态生成的Bitmap(宽高都是400)。
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
这是我的第一个小部件应用程序 :) 我有点迷路了,不明白为什么会有这个额外的填充。有人有想法吗?
感谢您的帮助。
来自App Widget Design Guidelines | Android Developers
When your widget is added, it will be stretched to occupy the minimum number of cells, horizontally and vertically, required to satisfy its minWidth and minHeight constraints.
每台设备的主屏幕上都有一个网格,小部件与之强制对齐。您甚至可以在某些设备上看到,在纵向和横向之间旋转时,网格单元格的纵横比会发生变化。
上面 link 的文档对如何指定小部件的大小有一些建议。那里的一些信息可能会对您有所帮助。
我建议,如果您只显示 ImageView
,而不是使用 RelativeLayout
,请使用重力为 center
和透明的 FrameLayout
背景。这样 FrameLayout
可能会被拉伸,但你的 ImageView
应该保持正方形。
更好的是,您可能根本不需要 ViewGroup
子类。只需将 ImageView
作为您的顶级组件并使用 FIT_CENTER
的比例类型。然后你的位图应该在 ImageView
内正方形。 (哦,不要为 ImageView
指定背景颜色,只需在位图中指定背景颜色即可。)
我的应用程序中有以下小部件,它包含一个 ImageView
,宽度和高度设置为 WRAP_CONTENT
。根视图再次是 RelativeLayout
,宽度和高度设置为 WRAP_CONTENT
。
我的问题是为什么小部件的顶部和底部有一些填充(标记为黑色)?我希望它是完美的正方形。以下是我的小部件布局。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/widget_battery_2x2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#000000"
android:layout_margin="@dimen/widget_margin"
android:padding="@dimen/widget_padding" >
<ImageView
android:id="@+id/image_widget_arc_battery"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ff0000"
android:layout_alignParentLeft="true"/>
</RelativeLayout>
@dimen/widget_margin
且 @dimen/widget_padding
为零。
ImageView
包含一个动态生成的Bitmap(宽高都是400)。
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
这是我的第一个小部件应用程序 :) 我有点迷路了,不明白为什么会有这个额外的填充。有人有想法吗?
感谢您的帮助。
来自App Widget Design Guidelines | Android Developers
When your widget is added, it will be stretched to occupy the minimum number of cells, horizontally and vertically, required to satisfy its minWidth and minHeight constraints.
每台设备的主屏幕上都有一个网格,小部件与之强制对齐。您甚至可以在某些设备上看到,在纵向和横向之间旋转时,网格单元格的纵横比会发生变化。
上面 link 的文档对如何指定小部件的大小有一些建议。那里的一些信息可能会对您有所帮助。
我建议,如果您只显示 ImageView
,而不是使用 RelativeLayout
,请使用重力为 center
和透明的 FrameLayout
背景。这样 FrameLayout
可能会被拉伸,但你的 ImageView
应该保持正方形。
更好的是,您可能根本不需要 ViewGroup
子类。只需将 ImageView
作为您的顶级组件并使用 FIT_CENTER
的比例类型。然后你的位图应该在 ImageView
内正方形。 (哦,不要为 ImageView
指定背景颜色,只需在位图中指定背景颜色即可。)