addContentView到GLSurfaceView问题

时间:2010-10-30 17:15:46

标签: android

我正在尝试在我的opengl视图上添加由xml定义的新LinearLayout。

我使用纯java:

public class VortexView extends GLSurfaceView {
 public boolean onTouchEvent(final MotionEvent event) {
        show_something();
 }

void show_something()
{
  //context is the main activity object that gets passed into this
  LinearLayout ll = new LinearLayout(context);
  b = new Button(context);
  b.setText("hello world 1");
  ll.addView(b);
  ll.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);
  context.addContentView(ll, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
}
}

但是我希望能够做到这一点:

LinearLayout ll = (LinearLayout)findViewById(R.layout.main);
context.addContentView(ll, new LayoutParams(LayoutParams.FILL_PARENT,  LayoutParams.FILL_PARENT));

我的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" 
          android:orientation="vertical" >
<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="10dp"
    android:text="kill"
    />
</LinearLayout>

每次我这样做虽然应用程序崩溃了。有什么想法吗?

2 个答案:

答案 0 :(得分:1)

你可以试试以下:

LayoutInflater inflater = getLayoutInflater();
getWindow().addContentView(inflater.inflate(R.layout.main, null), new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));

这应该有用......

请参阅:addContentView example simplified

答案 1 :(得分:1)

你有可能在那里弄​​得一团糟吗? 让我们以不同的方式尝试,在后台创建GLSurfaceView,并在其上添加LinearLayout

main.xml文件:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout 
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/relativeLayout"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <path.to.vortex.VortexView
            android:id="@+id/vortexView"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true" >
        </path.to.vortex.VortexView>

 <LinearLayout
            android:id="@+id/linearLayout"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" >
   </LinearLayout>
</RelativeLayout>

VortexView类中添加构造函数和初始化

  public class VortexView extends GLSurfaceView {

    public VortexView (Context context)
    {
        super(context);
        init(context);
    }

    public VortexView (Context context, AttributeSet atts)
    {
        super(context, atts);
        init(context);
    }

    public VortexView (Context context, AttributeSet atts, int defStyle)
    {
        super(context, atts, defStyle);
        init(context);
    }

    public void init(context)
    {
        //do whatever you need to init your view here...
    }
}

在您的活动中,您需要在main.xml上setContentView()

public class VortexActivity extends Activity
{
    Context mContext;
    VortexView mVortexView;
    RelativeLayout relativeLayout;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        mContext = getApplicationContext();
        setContentView(R.layout.main);

        mVortexView = (VortexView) findViewById(R.id.vortexView);
        mRelativeLayout = (RelativeLayout) findViewById(R.id.relativeLayout);
    }
}

现在您可以随意使用布局执行任何操作,并且由于VortexViewLinearLayout之前在xml中声明,因此它将位于其背景中。