2014-04-25 2 views
1

처음에는 내용보기를 SurfaceView를 확장하는 새로운 클래스로 설정했습니다 (예제 참조).SurfaceView 위에 레이아웃 추가하기

RenderView surfaceView = new RenderView(); 
setContentView(surfaceView); 

하지만 팽창 내가보기 수에 뭔가를 추가하고 첫 번째 레이어를 요구하고 나에게 오류가 발생하기 때문에 내가처럼 팽창 할 때, XML 파일의 레이아웃을 추가 할 수없는 것 정상적인 방법으로 할 것입니다.

그렇다면 SurfaceView 위에 .xml 파일의 레이아웃을 추가하는 방법은 무엇입니까?

이 RenderView 클래스의 코드의 중요한 부분입니다 : 당신의 Activity

public class RenderView extends SurfaceView implements Runnable { 

Context context; 
Thread thread = null; 

SurfaceHolder holder; 
boolean running = false; 

public RenderView(Context context) { 
    super(context); 
    this.context = context; 
    holder = getHolder(); 
} 

public void run() { 
    while (running) { 
     if(!holder.getSurface().isValid()) continue; 
     Canvas c = holder.lockCanvas(); 


     holder.unlockCanvasAndPost(c); 
    } 
} 
+0

해당 RenderView에 View()를 추가하면 어떻게됩니까? – shkschneider

+0

.xml 파일에서로드하려고합니다. – ViliX64

+0

null을 LayoutInflater에 전달할 수 있어야합니다. 오류가 발생했으며 스택 추적을 게시하십시오. – shkschneider

답변

2

콜이 방법 :

http://developer.android.com/reference/android/app/Activity.html#addContentView(android.view.View, android.view.ViewGroup.LayoutParams)

View view 매개 변수가 여러분의 XML 레이아웃을 팽창하여 얻을 수 있습니다 파일

LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); 
View secondLayerView = LayoutInflater.from(this).inflate(R.layout.my_layout, null, false); 
addContentView(secondLayerView, lp); 
1

xml로 원하면 프레임 레이아웃으로 서페이스 뷰를 둘러 쌉니다. 내 게임에서 이런 짓을 한 내가 그것에 점수와 시간에 최고 바를 오버레이했습니다

<LinearLayout 
    android:id="@+id/topBar" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" > 

    <TextView 
     android:id="@+id/scoreText" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:background="#F7F7F7" 
     android:ems="10" 
     android:gravity="center" 
     android:paddingLeft="5dp" 
     android:textColor="#000000" 
     android:textSize="20sp" > 
    </TextView> 

    <TextView 
     android:id="@+id/timeText" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:background="#F7F7F7" 
     android:ems="10" 
     android:gravity="center" 
     android:paddingLeft="5dp" 
     android:textColor="#000000" 
     android:textSize="20sp" > 
    </TextView> 
</LinearLayout> 

변경/여러분의 필요에 높이와 위치 등을 조정할.

관련 문제