当前位置: 动力学知识库 > 问答 > 编程问答 >

android - Getting xml layout from Parent View

问题描述:

I have a Game XML layout which extends a View, however I am not sure how I can get XML layout values within my GameView, please see code below:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="fill_parent"

android:layout_height="fill_parent" >

<com.llpartners.sanctus.GameView

android:id="@+id/gameview"

android:layout_width="match_parent"

android:layout_height="wrap_content" />

<RelativeLayout

android:id="@+id/vidContainerBattle"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:visibility="invisible" >

<VideoView

android:id="@+id/vvBeforeBattle"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_alignParentLeft="true"

android:layout_alignParentRight="true"

android:layout_alignParentBottom="true"

android:layout_alignParentTop="true"/>

</RelativeLayout>

</RelativeLayout>

So what I want to do is get my VideoView and RelativeLayout within my GameView class. I have tried the standard findViewById(R.id.vvBeforeBattle), however, as expected, this return a null pointer. Is there any way I can get this within my Gameview.class code?

UPDATE*

private void playVideo()

{

String path = "android.resource://" + context.getPackageName() + "/" + R.raw.test;

Uri vid = Uri.parse(path);

VideoView vvBeforeBattle = (VideoView)findViewById(R.id.vvBeforeBattle);

vvBeforeBattle.setVideoURI(vid);

vvBeforeBattle.start();

}

网友答案:

use below code in GameView class

((Activity)context).findViewById(R.id.vvBeforeBattle);
网友答案:

There is a lot of options for this job:

1.In your GameView view class get access to its parent and then get reference to your desired view

((View) getParent()).findViewById(...);

2.Get access to the parent Activity and then use its findViewById method, as @zohreh stated

3.Add a method to your GameView class something like setVideoView() and then call it in your Activity context and pass the proper reference to it.

As my experience in programming, the 3rd approach is better.

网友答案:

You are not showing your java code so I have to make a few assumptions here. But I believe the reason you are getting a null pointer is you are not setting the contentView layout first. Go ahead and add this to your onCreate() function before trying findViewById.

setContentView(R.layout.myxmllayout);

EDIT: My bad I understood your question wrong. if you include your RelativeLayout as a child of GameView your problem should be solved as below:

<com.llpartners.sanctus.GameView 
        android:id="@+id/gameview" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content">

    <RelativeLayout
        android:id="@+id/vidContainerBattle"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:visibility="invisible" >
        <VideoView
            android:id="@+id/vvBeforeBattle"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentBottom="true"
            android:layout_alignParentTop="true"/>
    </RelativeLayout>
</com.llpartners.sanctus.GameView>
分享给朋友:
您可能感兴趣的文章:
随机阅读: