运行时设置 TextView 在 RemoteView 中的位置

发布于 2024-11-10 03:00:22 字数 791 浏览 1 评论 0原文

亲爱的大家,我正在尝试在应用程序小部件中设置 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心凉怎暖 2024-11-17 03:00:22
在回答时,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)

瀟灑尐姊 2024-11-17 03:00:22
LayoutParams lp = myRemoteView.getLayoutParams();
lp.marginLeft = 5; 
myRemoteView.setLayoutParams(lp);
LayoutParams lp = myRemoteView.getLayoutParams();
lp.marginLeft = 5; 
myRemoteView.setLayoutParams(lp);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文