Change layout Margin from within a thread on Android
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
精彩评论