运行时设置 TextView 在 RemoteView 中的位置
亲爱的大家,我正在尝试在应用程序小部件中设置 TextView 的位置。
基本上,直接访问 TextView 的属性是有效的:
myRemoteView.setTextColor(R.id.myTextView, Color.WHITE); //works
同样间接地,我可以访问 TextView 属性:
myRemoteView.setInt(R.id.myTextView, "setTextColor", Color.BLUE); // works
并且设置浮点值也有效:
myRemoteView.setFloat(R.id.myTextView, "setTextSize", 25); // works
现在我尝试将 TextView 的 x 位置移动 5 个像素:
myRemoteView.setFloat(R.id.myTextView, "setTranslationX", 5); // does not work
myRemoteView.setFloat(R.id.myTextView, "setX", 5); // does not work
myRemoteView.setInt(R.id.myTextView, "setLeft", 5); // does not work
我尝试了 FrameLayout 和relativelayout,还有absolutelayout(已弃用)。什么都不起作用。但一定有办法做到这一点。你能帮忙吗?
Dear all, I am trying to set the position of a TextView within an appwidget.
Basically, the direct access to properties af the TextView works:
myRemoteView.setTextColor(R.id.myTextView, Color.WHITE); //works
Also indirectly, I can access the TextView properties:
myRemoteView.setInt(R.id.myTextView, "setTextColor", Color.BLUE); // works
And setting float values also works:
myRemoteView.setFloat(R.id.myTextView, "setTextSize", 25); // works
Now I'm trying to shift the x position of the TextView by 5 pixels:
myRemoteView.setFloat(R.id.myTextView, "setTranslationX", 5); // does not work
myRemoteView.setFloat(R.id.myTextView, "setX", 5); // does not work
myRemoteView.setInt(R.id.myTextView, "setLeft", 5); // does not work
I tried FrameLayout and RelativeLayout, and also AbsoluteLayout (depreciated). Nothing works. But there must be a way to do that. Can you please help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在回答时,RemoteViews API 不允许您设置平移或位置
但是,您可以通过设置 TextView 父级的填充来实现所需的效果
RemoteViews#setViewPadding(int, int, int, int, int , int)
或者,从 API 31 开始,您还可以设置 TextView 的边距
RemoteViews#setViewLayoutMargin(int,整型、浮点型、整型)
At the time of the answer, the RemoteViews API does not allow you to set translation or position
However, you can achieve the desired effect by setting the padding for the parent of your TextView
RemoteViews#setViewPadding(int, int, int, int, int, int)
Or, since API 31, you can also set the margin for your TextView
RemoteViews#setViewLayoutMargin(int, int, float, int)