在surfaceview顶部的片段

时间:2015-08-03 18:57:36

标签: android android-layout android-fragments surfaceview

您好我的app中有一个surfaceview。我的所有游戏都是这样构建的:

实现Surfaceview的子类,实现SurfaceHolder.Callback接口。

现在是我想点击按钮(自定义对象)时打开菜单  这应该会带来一个带有可滚动文本视图的片段(我只想到一个列表视图,里面有一个textview元素,因为我认为如果文本很长,单独的textview就不可滚动了。欢迎任何相反的参数)

但我该怎么做?

我已经完成了Fragment类和片段的布局

编辑3:

好的,我已经设法以编程方式设置SurfaceView:

   protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if(savedInstanceState==null)
    {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                             WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.main);
        prefs=getSharedPreferences("myPrefs",0);
        mView=new MainView(this);
        loadSharedPrefs();

        LinearLayout myLayout = (LinearLayout)findViewById(R.id.main);

        mView.setLayoutParams(new LinearLayout.LayoutParams(
                                             LinearLayout.LayoutParams.MATCH_PARENT,
                                             LinearLayout.LayoutParams.MATCH_PARENT));
        myLayout.addView(mView);
    }
}

其中布局只是一个空白的线性布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" 
android:id="@+id/main">
</LinearLayout>

所以现在我只需要在单击按钮时显示片段

到目前为止片段类:

public class HintListFragment extends Fragment
{


 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.hintlist, container, false);
    }
}

以及此片段的布局:

<?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" >

<TextView 
    android:id="@+id/hintList"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:text="sagdjsahgdhascxasbcxahsgdhsagdhasgdashgdsajsgdh"

    />


</LinearLayout>

1 个答案:

答案 0 :(得分:0)

在布局中,将surfaceview和片段包装在FrameLayout中。确保片段位于surfaceview的顶部,然后将片段的可见性设置为GONE。如果要显示片段,只需将可见性设置为VISIBLE

即可
<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <SurfaceView
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
    <com.example.MyFragment
        android:visibility="gone"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:layout_gravity="center"
        android:id="@+id/myfragment"/>
</FrameLayout>