Как использовать мой SurfaceView с main.xml на Android? - PullRequest
4 голосов
/ 03 августа 2011

В основном классе моего проекта у меня есть

public class MyClass extends Activity{
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        mySurfaceView = new MySurfaceView(this);
        setContentView(mySurfaceView); 
        ...
    }

    class MySurfaceView extends SurfaceView implements Runnable{
       ...
    }
}

Я хочу добавить кнопки и колеса к этой программе, которые у меня есть в отдельной мини-программе прямо сейчас с файлом main.xml,Я знаю, что для доступа к этому, у меня должна быть строка setContentView (R.layout.main) , но затем я теряю доступ к своему SurfaceView.Как мне сделать так, чтобы у меня были оба?В Интернете я увидел кое-что о добавлении нескольких строк с помощью FrameLayout, но я не могу понять, где именно, и я попытался вставить его в различные места в моем main.xml, но это просто приводит к разрыву всей моей программы ..

В настоящее время мой main.xml выглядит так:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
 <Button android:id="@+id/selectButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="Select Time Interval"
android:onClick="selfDestruct"
/>
<Button android:id="@+id/finishButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="Done"
android:visibility="gone"
android:onClick="selfDestruct"
/>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:layout_gravity="top|left"
    android:paddingLeft="12dp"
    android:paddingRight="12dp"
    >
    <kankan.wheel.widget.WheelView android:id="@+id/startHour"
        android:visibility="gone"
        android:layout_height="wrap_content"
        android:layout_width="43dp"/>
    <kankan.wheel.widget.WheelView android:id="@+id/startMins"
        android:visibility="gone"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"/>
        <kankan.wheel.widget.WheelView android:id="@+id/startAmpm"
        android:visibility="gone"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"/>

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent" >
        <TextView android:id="@+id/theWordTo"
            android:visibility="gone"
            android:layout_width="wrap_content" 
            android:layout_height="fill_parent"
            android:gravity="center"
            android:text="   to   "/>
        </LinearLayout>

        <kankan.wheel.widget.WheelView android:id="@+id/endHour"
        android:visibility="gone"
        android:layout_height="wrap_content"
        android:layout_width="43dp"
        />
    <kankan.wheel.widget.WheelView android:id="@+id/endMins"
        android:visibility="gone"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"/>
        <kankan.wheel.widget.WheelView android:id="@+id/endAmpm"
        android:visibility="gone"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"/>
</LinearLayout>

</LinearLayout>

Ответы [ 2 ]

8 голосов
/ 03 августа 2011

Я думаю, вы хотите использовать FrameLayout . Измените ваш main.xml, чтобы он выглядел так:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<kankan.current.app.MyClass.MySurfaceView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/surfaceView" />

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
 <Button android:id="@+id/selectButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="Select Time Interval"
android:onClick="selfDestruct"
/>
<Button android:id="@+id/finishButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="Done"
android:visibility="gone"
android:onClick="selfDestruct"
/>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:layout_gravity="top|left"
    android:paddingLeft="12dp"
    android:paddingRight="12dp"
    >
    <kankan.wheel.widget.WheelView android:id="@+id/startHour"
        android:visibility="gone"
        android:layout_height="wrap_content"
        android:layout_width="43dp"/>
    <kankan.wheel.widget.WheelView android:id="@+id/startMins"
        android:visibility="gone"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"/>
        <kankan.wheel.widget.WheelView android:id="@+id/startAmpm"
        android:visibility="gone"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"/>

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent" >
        <TextView android:id="@+id/theWordTo"
            android:visibility="gone"
            android:layout_width="wrap_content" 
            android:layout_height="fill_parent"
            android:gravity="center"
            android:text="   to   "/>
        </LinearLayout>

        <kankan.wheel.widget.WheelView android:id="@+id/endHour"
        android:visibility="gone"
        android:layout_height="wrap_content"
        android:layout_width="43dp"
        />
    <kankan.wheel.widget.WheelView android:id="@+id/endMins"
        android:visibility="gone"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"/>
        <kankan.wheel.widget.WheelView android:id="@+id/endAmpm"
        android:visibility="gone"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"/>
</LinearLayout>

</LinearLayout>

</FrameLayout>

Тогда в в деятельности:

public class MyClass extends Activity{
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main); 
        mySurfaceView = (MySurfaceView) findViewById(R.id.surfaceView);
    }

    class MySurfaceView extends SurfaceView implements Runnable{
       ...
    }
}
1 голос
/ 03 августа 2011

Конечно, можно добавить свой SurfaceView в макет во время выполнения (например, в методе onCreate()), но, вероятно, было бы гораздо проще добавить его в макет XML.

Для простоты я сначала определил бы MySurfaceView в своем собственном файле, а не в том же файле, что и ваша активность MyClass. Убедитесь, что вы включили конструктор в форме MySurfaceView(Context context, AttributeSet attrs), чтобы представление могло быть правильно раздуто. Затем просто вставьте следующее в свой XML-макет, где вы хотите.

<com.example.package.MySurfaceView
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>

Где первая строка - путь к MySurfaceView (com.example.package - имя пакета, а MySurfaceView - файл, в котором вы определили пользовательское представление).

...