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);
}
}
代码如上,我用xml以外的Java代码实现布局,添加一个TextView,tv。使用 tv.setGravity(Gravity.CENTER) 定位它,为什么我不能更改电视位置。我在等你的答复。先感谢您。我会感激你的好意。