2010-10-30 60 views
1

我正在試圖添加一個新的LinearLayout在我的opengl視圖之上由xml定義。addContentView到GLSurfaceView問題

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> 

每次我

我用純Java有這方面的工作儘管應用程序崩潰,請執行此操作。有任何想法嗎?

回答

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類添加構造函數和initilazation

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

在您的活動,您需要setContentView()您的main.xml:

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); 
    } 
} 

現在你可以隨意做任何你想要的佈局,並且由於VortexView在xml中被聲明,所以它將在LinearLayout之前。

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