开发者

Change layout Margin from within a thread on Android

开发者 https://www.devze.com 2023-02-19 02:25 出处:网络
I have a game running in a thread, and I want to modify the margins of a view from within the thread.

I have a game running in a thread, and I want to modify the margins of a view from within the thread.

The following code works fine for changing the Margins of the view from the main Activity, but I need to change them from 开发者_运维问答within the thread after an AlertDialog is closed.

View view = (View) findViewById(R.id.viewname);
ViewGroup.MarginLayoutParams mlp = (ViewGroup.MarginLayoutParams) View.getLayoutParams();
mlp.setMargins(0, 10, 0, 0);

BUT, in the thread, the same code just returns null pointer exceptions.

I'm not sure what to do.


Put a function in your Activity to modify the margins. Then call it from your Thread. You can see an example using Handler and Runnable here

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号