public class MainActivity extends Activity implements OnGestureListener
{
private LinearLayout mainlayout;
private TextView tv;
private GestureDetector gestureScanner;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
gestureScanner = new GestureDetector(MainActivity.this,this);
mainlayout = new LinearLayout(this);
mainlayout.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
mainlayout.setBackgroundColor(Color.GREEN);
tv = new TextView(getApplicationContext());
tv.setBackgroundColor(Color.YELLOW);
tv.setTextColor(Color.BLUE);
tv.setGravity(Gravity.CENTER); //here, why tv located top-left
tv.setTextSize(20);
tv.setLayoutParams(new LinearLayout.LayoutParams(320,80));
mainlayout.addView(tv);
setContentView(mainlayout);
}
}
Code is as above, I realize layout in Java code other than xml, adding a TextView, tv. Using tv.setGravity(Gravity.CENTER) to locate it, why I can't change the tv location. I'm waiting for your answer. Thank you in advance. I will appreciate your kindness.