Android问题:String types not allowed (at 'layout_gravity' with value 'top/right').

本文纠正了在Android布局文件中使用layout_gravity属性时的一个常见错误,并详细解释了该属性的正确用法及所有可选项。
android:layout_gravity="top/right"
出错的是这一句。
实际上应该是:
android:layout_gravity="top|right",这里的top和right的关系是并行的。
再说说layout_gravity的用法:
<p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">属性可选的值有:</p><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">top、bottom、left、right、center_vertical、fill_vertical、center_horizontal、fill_horizontal、center、fill、clip_vertical。</p><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">而且这些属性是可以多选的,用“|”分开。</p><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">默认这个的值是:Gravity.LEFT</p><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">来自:</p><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">http://blog.youkuaiyun.com/wanderocn/article/details/6424876
</p>
提示我:@layout/ item_widget includes views not allowed in a RemoteView, 但我的item_widget中并未发现此类控件 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/ll_site" android:layout_width="match_parent" android:layout_height="wrap_content" android:focusable="true" android:gravity="center_horizontal" android:orientation="vertical"> <FrameLayout android:layout_width="@dimen/tpds_all_dp_64" android:layout_height="wrap_content"> <ImageView android:id="@+id/iv_site" android:layout_width="match_parent" android:layout_height="wrap_content" tools:src="@drawable/icon_sdn_home_site" /> <LinearLayout android:id="@+id/alert_icon" android:layout_width="wrap_content" android:layout_height="@dimen/tpds_all_dp_16" android:layout_gravity="top|end" android:layout_marginStart="-10dp" android:layout_marginTop="@dimen/tpds_all_dp_1" android:background="@drawable/circular_corner_red_8dp" android:gravity="center_vertical" android:orientation="horizontal"> <ImageView android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_gravity="center_vertical" android:layout_marginStart="@dimen/tpds_all_dp_3" android:src="@drawable/icon_warning_mini" /> <TextView android:id="@+id/alert_num" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginEnd="@dimen/tpds_all_dp_5" android:background="@drawable/circular_corner_red_8dp" android:gravity="start" android:textColor="@color/color_white100" android:textSize="@dimen/text_size_12" tools:text="1" /> </LinearLayout> </FrameLayout> <LinearLayout android:id="@+id/ll_num" android:layout_width="wrap_content" android:layout_height="wrap_content"> <FrameLayout android:id="@+id/view_space" android:layout_width="16dp" android:layout_height="16dp" /> <TextView android:id="@+id/tv_online" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="@dimen/text_size_14" tools:text="N" tools:textColor="@color/omada_color_green" /> <TextView android:id="@+id/div" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="/" android:textColor="?android:textColorPrimary" android:textSize="@dimen/text_size_14" /> <TextView android:id="@+id/tv_total" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="?android:textColorPrimary" android:textSize="@dimen/text_size_14" tools:text="A" /> <ImageView android:id="@+id/alert_attention" android:layout_width="16dp" android:layout_height="16dp" android:layout_marginStart="6dp" android:src="@drawable/icon_info_yellow" /> </LinearLayout> <TextView android:id="@+id/tv_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:text="@string/global_sites" android:textColor="@color/color_text_secondary_1" android:textSize="@dimen/text_size_12" /> </LinearLayout>
最新发布
10-13
你遇到的错误提示: ``` @layout/item_widget includes views not allowed in a RemoteView ``` 说明你在 **AppWidget(桌面小部件)** 中使用了 `item_widget.xml` 作为列表项布局(比如在 `StackView` 或 `ListView` 中),但其中包含了 **RemoteViews 不支持的控件或布局结构**。 虽然你的 `item_widget.xml` 看起来只用了 `LinearLayout`、`FrameLayout`、`ImageView`、`TextView` 这些常见控件,这些控件本身是支持的,但问题可能出在 **嵌套层级过深** 或者 **某些属性/子控件组合不被 RemoteViews 允许**。 --- ### ✅ 常见 RemoteViews 限制回顾 RemoteViews 在 AppWidget 中能使用的控件非常有限,且有严格限制: #### ✅ 支持的容器: - `LinearLayout` - `FrameLayout` - `RelativeLayout`(部分支持,推荐用 LinearLayout) > ❌ 不支持:`ConstraintLayout`, `GridLayout`, `CoordinatorLayout`, `TableLayout` 等复杂布局。 #### ✅ 支持的子控件: - `TextView` - `ImageView` - `ImageButton` - `Button` - `ProgressBar` - `Chronometer` - `AnalogClock` - `ViewStub` > ❌ 不支持:`EditText`, `WebView`, `RecyclerView`, `ListView`, `ScrollView`, 自定义 View 等。 #### ⚠️ 特别注意: - 所有嵌套布局不能太深(建议 ≤ 3~4 层) - 某些属性会导致 RemoteViews 解析失败(如 `tools:` 属性不会影响运行时,但布局结构必须合法) - `android:background` 使用 Drawable 是允许的,但如果引用的是 **LayerDrawable / StateListDrawable 复杂资源**,也可能导致问题 - `android:layout_marginStart="-10dp"` 负 margin 可能在某些系统版本中引起渲染异常(虽然不是直接禁止,但应避免) --- ### 🔍 分析你的布局潜在问题 ```xml <LinearLayout> <FrameLayout> <ImageView /> <LinearLayout> <!-- 提示图标 --> <ImageView /> <TextView /> </LinearLayout> </FrameLayout> <LinearLayout> <!-- 数量显示 --> <FrameLayout /> <TextView /> <TextView /> <TextView /> <ImageView /> </LinearLayout> <TextView /> </LinearLayout> ``` #### 存在的问题点: 1. **嵌套层数较深** - 外层 `LinearLayout` - 内含 `FrameLayout` - 内含 `LinearLayout`(alert_icon) - 再一个 `LinearLayout`(ll_num) - 包含多个子 view - 总体深度达到 4~5 层,在 RemoteViews 中属于“偏深”,可能导致性能问题或兼容性问题。 2. **负 margin 使用:** ```xml android:layout_marginStart="-10dp" ``` - 虽然 XML 合法,但在 RemoteViews 中不推荐使用负边距,可能导致布局错乱甚至崩溃。 3. **`tools:text` 和 `tools:src` 没问题,但确保没有遗漏真实数据绑定逻辑** 4. **`alert_icon` 中的 TextView 使用了 background:** ```xml android:background="@drawable/circular_corner_red_8dp" ``` - 如果这个 drawable 是普通的 shape drawable,没问题。 - 但如果它是 `<selector>` 或包含 padding/state,某些旧设备可能有问题。 5. **最可疑的是:`FrameLayout` + 多个重叠子 view 实现“角标”效果** - RemoteViews 对 `FrameLayout` 的支持有限,尤其是涉及定位(`layout_gravity="top|end"`)时 - `android:layout_gravity="top|end"` 在 RemoteViews 中不一定可靠 --- ### ✅ 解决方案与修改建议 #### ✅ 推荐改写方式:简化布局,避免 FrameLayout 重叠 + 负 margin 使用 `RelativeLayout` 或更扁平化的 `LinearLayout` + `drawableEnd` 替代复杂的嵌套。 #### ✅ 修改后的推荐布局(适配 RemoteViews): ```xml <!-- res/layout/item_widget.xml --> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/ll_site" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:gravity="center_horizontal" android:focusable="true"> <!-- 主图 + 角标:使用 RelativeLayout 避免负 margin --> <RelativeLayout android:layout_width="64dp" android:layout_height="wrap_content"> <ImageView android:id="@+id/iv_site" android:layout_width="match_parent" android:layout_height="wrap_content" android:contentDescription="@string/site_image" /> <!-- 角标容器 --> <LinearLayout android:id="@+id/alert_icon" android:layout_width="wrap_content" android:layout_height="16dp" android:layout_alignTop="@id/iv_site" android:layout_alignEnd="@id/iv_site" android:layout_marginEnd="-8dp" android:layout_marginTop="2dp" android:background="@drawable/circular_corner_red_8dp" android:orientation="horizontal" android:paddingStart="4dp" android:paddingEnd="2dp" android:gravity="center_vertical"> <ImageView android:layout_width="10dp" android:layout_height="10dp" android:src="@drawable/icon_warning_mini" android:layout_marginEnd="1dp" /> <TextView android:id="@+id/alert_num" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="12sp" android:textColor="@color/color_white100" android:includeFontPadding="false" android:singleLine="true" tools:text="1" /> </LinearLayout> </RelativeLayout> <!-- 在线数量行 --> <LinearLayout android:id="@+id/ll_num" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" android:gravity="center_vertical" android:layout_marginTop="4dp"> <View android:layout_width="16dp" android:layout_height="16dp" /> <TextView android:id="@+id/tv_online" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="14sp" tools:text="N" tools:textColor="@color/omada_color_green" /> <TextView android:id="@+id/div" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="/" android:textColor="?android:textColorPrimary" android:textSize="14sp" /> <TextView android:id="@+id/tv_total" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="?android:textColorPrimary" android:textSize="14sp" tools:text="A" /> <ImageView android:id="@+id/alert_attention" android:layout_width="16dp" android:layout_height="16dp" android:layout_marginStart="6dp" android:src="@drawable/icon_info_yellow" /> </LinearLayout> <!-- 名称 --> <TextView android:id="@+id/tv_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="2dp" android:text="@string/global_sites" android:textColor="@color/color_text_secondary_1" android:textSize="12sp" /> </LinearLayout> ``` --- ### ✅ 关键修改点解释: | 修改项 | 原因 | |------|------| | 将外层 `FrameLayout` 替换为 `RelativeLayout` | 更清晰控制子 view 定位,避免 `FrameLayout` + `gravity` 的不确定性 | | 移除 `layout_marginStart="-10dp"` | 负 margin 不推荐用于 RemoteViews,改为 `marginEnd="-8dp"` 并配合 `alignEnd` | | 使用 `RelativeLayout` 控制定位 | `layout_alignTop` + `alignEnd` 实现角标定位,比 `FrameLayout` + gravity 更可靠 | | 简化嵌套 | 减少不必要的 `FrameLayout` 包裹 | | 添加 `includeFontPadding="false"` 到 TextView | 避免文字上下留白过多 | | 使用 `View` 替代空 `FrameLayout` | 更语义化 | --- ### ✅ 注意事项 - 所有 `@drawable` 资源必须是静态资源(不能是动画或复杂 selector) - 在 `AppWidgetProvider` 中更新视图时,只能调用 `RemoteViews.set...()` 方法 - 测试时务必在真机上测试,模拟器有时不报错但真机崩溃 --- ### ✅ 如何验证是否修复? 1. 清理并重新生成项目(避免缓存) 2. 卸载并重新添加 widget 3. 查看 Logcat 是否还有 `Cannot bind to view` 或 `Error inflating RemoteViews` 错误 --- ### ✅ 补充建议:远程视图调试技巧 你可以通过以下代码打印 RemoteViews 加载异常: ```java try { views = new RemoteViews(context.getPackageName(), R.layout.item_widget); // 设置各种属性... } catch (Exception e) { Log.e("Widget", "Failed to inflate RemoteViews", e); } ``` --- ###
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值