正确设置Android Home小部件大小的样式
根据用户将小部件扩展到的行数,我尝试在我的主页小部件中显示最多三个元素 所以每个元素的高度应该是一行的高度。主要目标是让它在每个设备上看起来都正确 这是我的布局:正确设置Android Home小部件大小的样式,android,android-widget,Android,Android Widget,根据用户将小部件扩展到的行数,我尝试在我的主页小部件中显示最多三个元素 所以每个元素的高度应该是一行的高度。主要目标是让它在每个设备上看起来都正确 这是我的布局: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/shape_roundedcorners_white">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/article1"
android:layout_width="match_parent"
android:layout_height="40dp"
android:orientation="horizontal"
android:layout_margin="@dimen/widget_margin"
android:background="#0000ff">
<ImageView
android:id="@+id/article1img"
android:layout_width="35dp"
android:layout_height="35dp"
android:gravity="center"
android:layout_gravity="center"
android:layout_marginRight="15dp"
android:layout_marginLeft="10dp"
android:src="@drawable/ic_notifications" />
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_margin="@dimen/inner_widget_margin">
<TextView
android:id="@+id/article1Title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:contentDescription="@string/appwidget_text"
android:gravity="left"
android:text="@string/appwidget_title"
android:textColor="#000000"
android:textSize="14sp"
android:textStyle="bold"/>
<TextView
android:id="@+id/article1Text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:contentDescription="@string/appwidget_text"
android:gravity="left"
android:text="@string/appwidget_text"
android:textColor="#000000"
android:textSize="12sp"/>
</LinearLayout>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/divider"
android:layout_width="match_parent"
android:layout_height="1dp"
android:orientation="vertical"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
style="@style/Divider">
<!--<View style="@style/Divider"/>-->
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/article2"
android:layout_width="match_parent"
android:layout_height="85dp"
android:orientation="horizontal"
android:layout_margin="@dimen/widget_margin"
android:background="#ffffff">
<ImageView
android:id="@+id/article2img"
android:layout_width="40dp"
android:layout_height="35dp"
android:gravity="center"
android:layout_gravity="center"
android:layout_marginRight="15dp"
android:layout_marginLeft="10dp"
android:src="@drawable/ic_notifications" />
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="35pt"
android:orientation="vertical"
android:layout_margin="@dimen/inner_widget_margin"
android:background="#ffffff">
<TextView
android:id="@+id/article2Title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:contentDescription="@string/appwidget_text"
android:gravity="left"
android:text="@string/appwidget_title"
android:textColor="#000000"
android:textSize="14sp"
android:textStyle="bold"/>
<TextView
android:id="@+id/article2Text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:contentDescription="@string/appwidget_text"
android:gravity="left"
android:text="@string/appwidget_text"
android:textColor="#000000"
android:textSize="12sp"/>
</LinearLayout>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/divider"
android:layout_width="match_parent"
android:layout_height="1dp"
android:orientation="vertical"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
style="@style/Divider">
<!--<View style="@style/Divider"/>-->
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/article3"
android:layout_width="match_parent"
android:layout_height="40dp"
android:orientation="horizontal"
android:layout_margin="@dimen/widget_margin"
android:background="#ffffff">
<ImageView
android:id="@+id/article3img"
android:layout_width="35dp"
android:layout_height="35dp"
android:gravity="center"
android:layout_gravity="center"
android:layout_marginRight="15dp"
android:layout_marginLeft="10dp"
android:src="@drawable/ic_notifications" />
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="35pt"
android:orientation="vertical"
android:layout_margin="@dimen/inner_widget_margin"
android:background="#ffffff">
<TextView
android:id="@+id/article3Title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:contentDescription="@string/appwidget_text"
android:gravity="left"
android:text="@string/appwidget_title"
android:textColor="#000000"
android:textSize="14sp"
android:textStyle="bold"/>
<TextView
android:id="@+id/article3Text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:contentDescription="@string/appwidget_text"
android:gravity="left"
android:text="@string/appwidget_text"
android:textColor="#000000"
android:textSize="12sp"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
但我得到的结果是这样的,将小部件放在minHeight上:
很明显,我的布局中一个元素的高度与屏幕上一个网格行的高度相差很大
我做错了什么,或者解决问题的标准方法是什么?
minHeight
和minWidth
是提示和建议,而不是要求。有数百个带有主屏幕的发射器,没有一个需要满足您的期望值或支持这么小的高度。您要求的尺寸大约是8:1宽高比;你得到的实际尺寸接近3:1
您的AppWidgetProvider
子类可以覆盖OnAppWidgetOptions Changed()
并检查提供的捆绑包
,以了解应用程序小部件的实际大小。这并不能保证每个主屏幕都会调用它,但这是你最好的选择。AppWidgetManager
上有OPTION\u APPWIDGET\u
键,可以在该包中。对于名为newOptions
的Bundle
参数,您可以尝试访问:
newOptions.getInt(AppWidgetManager.OPTION\u APPWIDGET\u MIN\u WIDTH)
newOptions.getInt(AppWidgetManager.OPTION\u APPWIDGET\u MAX\u WIDTH)
newOptions.getInt(AppWidgetManager.OPTION\u APPWIDGET\u MIN\u HEIGHT)
newOptions.getInt(AppWidgetManager.OPTION\u APPWIDGET\u MAX\u HEIGHT)
这些将是dp
中的值
从那里,您可以使用AppWidgetManager
发送一个新的remoteview
,它可能更适合主屏幕上显示的应用程序小部件的大小
通过一个显示自己大小范围的应用程序小部件来演示这一点。Thansk!最小/最大值的确切含义是什么。这真的能帮助我将小部件内容调整到正确的大小吗?我如何知道我应该将内容调整为最小还是最大?我也不知道如何更改线性布局的高度。文档说在Android 12之前使用LayoutParams.height
,但我不知道如何使用。@progNewbie:“最小/最大值到底是什么意思”——这意味着你的应用程序小部件的实际大小在宽度和高度的最小和最大值指定的范围内。“我如何知道我应该将内容大小调整为最小值还是最大值?”——这将是你的图形设计师的问题。好吧,所以人们无法真正知道确切的大小:/n你知道我如何设置我的RemoteView的大小吗?@prognebie:AFAIK,实际上,remoteview
本身类似于父容器,其大小设置为match\u parent
/match\u parent
,以填充应用了remoteview
的任何空间(应用程序小部件、自定义通知等)。
<dimen name="widget_margin">8dp</dimen>
<dimen name="inner_widget_margin">2dp</dimen>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid
android:color="#ffffff"/>
<corners
android:radius="12dp"/>
</shape>
<appwidget-provider
xmlns:android="http://schemas.android.com/apk/res/android"
android:initialKeyguardLayout="@layout/news_widget"
android:initialLayout="@layout/news_widget"
android:minHeight="40dp"
android:minWidth="300dp"
android:previewImage="@android:drawable/ic_menu_add"
android:resizeMode="horizontal|vertical"
android:updatePeriodMillis="2700000"
android:widgetCategory="home_screen">
</appwidget-provider>