引用:http://www.devdiv.com/thread-73074-1-1.html
LinearLayout.LayoutParams para=new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT );
para.setMargins(20, 20, 20, 20); //left,top,right, bottom
para.gravity = Gravity.RIGHT;
tv.setLayoutParams(para);
tv.setBackgroundResource(R.drawable.icon);