Activity的onCreate方法中通常利用布局文件来构建ContentView。另一种比较实用的方法是:在代码中动态创建ContentView。如下所示:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
gestureScanner = new GestureDetector(this);
gestureScanner.setOnDoubleTapListener(doubleTapListener);
main = new LinearLayout(this);
main.setBackgroundColor(Color.GRAY);
main.setLayoutParams(new LinearLayout.LayoutParams(320, 480));
main.setOrientation(LinearLayout.VERTICAL);
viewA = new TextView(this);
viewA.setBackgroundColor(Color.YELLOW);
viewA.setTextColor(Color.BLACK);
viewA.setTextSize(16);
viewA.setLayoutParams(new LinearLayout.LayoutParams(320, 50));
main.addView(viewA);
setContentView(main);
}
转载于:https://blog.51cto.com/truesea/1322493