0
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.

Mario Kutlev
  • 4,897
  • 7
  • 44
  • 62
Kailani Yung
  • 45
  • 1
  • 8

2 Answers2

2

You're setting the gravity of the content of the TextView with that code, not of the TextView itself. Try using:

LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT);
lp.gravity = Gravity.CENTER;
tv.setLayoutParams(lp);

EDIT

Turns out, it is possible with LinearLayout, but only works if you change the orientation and want only one TextView in the entire layout:

private LinearLayout mainlayout;
private TextView tv;

@Override
public void onCreate(Bundle bundle) {
    super.onCreate(bundle);

    mainlayout = new LinearLayout(this);
    mainlayout.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
    mainlayout.setBackgroundColor(Color.GREEN);
    mainlayout.setOrientation(LinearLayout.VERTICAL);

    tv = new TextView(getApplicationContext());
    tv.setBackgroundColor(Color.YELLOW);
    tv.setTextColor(Color.BLUE);
    tv.setTextSize(20);
    tv.setLayoutParams(new LinearLayout.LayoutParams(320, 80));
    mainlayout.addView(tv);
    setContentView(mainlayout);

    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
            LayoutParams.WRAP_CONTENT);
    lp.gravity = Gravity.CENTER;
    tv.setLayoutParams(lp);
    tv.setText("LOL");

}

The above code results in:

enter image description here

Raghav Sood
  • 81,899
  • 22
  • 187
  • 195
1
mainlayout = new RelativeLayout(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.setTextSize(20);
        tv.setGravity(Gravity.CENTER);

        tv2 = new TextView(getApplicationContext());
        tv2.setBackgroundColor(Color.YELLOW);
        tv2.setTextColor(Color.BLUE);
        tv2.setTextSize(20);
        tv2.setGravity(Gravity.CENTER);

        RelativeLayout.LayoutParams params= new RelativeLayout.LayoutParams(320, 80);
        params.addRule(RelativeLayout.CENTER_IN_PARENT);

        tv.setLayoutParams(params);

        RelativeLayout.LayoutParams params2= new RelativeLayout.LayoutParams(320, 80);
        tv2.setLayoutParams(params2);


        mainlayout.addView(tv);
        mainlayout.addView(tv2);

        setContentView(mainlayout);
Talha
  • 12,673
  • 5
  • 49
  • 68