我有一个程序,我想在一个 Activity 中添加两个 View ,比如
public class AnimationActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new GraphicsViewForBitmap(this));
setContentView(new GraphicsView(this));
}
}
其中 GraphicsViewForBitmap 和 GraphicsView 是两个扩展 View 的类。 所以我想同时将两个 View 设置为一个 Activity 。 是否可以? 请给我答案。 谢谢
最佳答案
setContentView()
将只显示您设置的 View 。如果您想显示多个 View ,那么您可以在布局 XML 文件中的任何布局中添加这两个 View ,例如 LinearLayout
、RelativeLayout
等。然后您可以使用 setContentView(R.layout.yourXML)
。
这里是你如何在你的 XML 中做到这一点......
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.yourpkg.GraphicsView
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
<com.yourpkg.GraphicsViewForBitmap
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
关于Android:如何向一个 Activity 添加两个 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6248177/