Set position of TextView in RemoteView during runt

2019-04-07 09:07发布

问题:

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?

回答1:

LayoutParams lp = myRemoteView.getLayoutParams();
lp.marginLeft = 5; 
myRemoteView.setLayoutParams(lp);