Android 在OnLongClick侦听器中设置ContentView
我在setContentView中遇到了一个问题,它向我抛出了一个空指针 在我的主机中,我得到了一个小的SurfaceView,它占据了大约50%的屏幕。 我想实现一种方法,如果你长时间点击它,它可以让SurfaceView全屏显示。我是这样做的:Android 在OnLongClick侦听器中设置ContentView,android,layout,onlongclicklistener,Android,Layout,Onlongclicklistener,我在setContentView中遇到了一个问题,它向我抛出了一个空指针 在我的主机中,我得到了一个小的SurfaceView,它占据了大约50%的屏幕。 我想实现一种方法,如果你长时间点击它,它可以让SurfaceView全屏显示。我是这样做的: final FrameLayout frame = (FrameLayout) findViewById(R.id.frame_layout); frame.setOnLongClickListener(new OnLongClickListener
final FrameLayout frame = (FrameLayout) findViewById(R.id.frame_layout);
frame.setOnLongClickListener(new OnLongClickListener() {
boolean clicked = false;
@Override
public boolean onLongClick(View v) {
if (clicked){
clicked = false;
(MyActivity.this).setContentView(R.layout.main);
} else {
clicked = true;
(MyActivity.this).setContentView(R.layout.fullScreen);
}
return true;
}
});
有人能帮我解决这个问题吗
问候
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="right"
android:orientation="vertical"
android:weightSum="1"
android:background="@drawable/background_new" >
<TextView
android:text="@string/measures"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/text_measures"
android:textColor="#ff444444"
android:layout_marginTop="30dp"
android:layout_marginLeft="10dp"
/>
<ImageButton
android:contentDescription="@string/desc_edge"
android:layout_toRightOf="@id/text_measures"
android:id="@+id/edges_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/edges_black"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:layout_marginBottom="30dp"
android:onClick="edge_handler"
android:background="@null"
/>
<FrameLayout
android:background="@xml/border"
android:id="@+id/camera_preview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="10dp"
android:layout_marginBottom="35dp"
android:layout_marginRight="200dp"
android:layout_marginTop="80dp"
android:layout_below="@id/text_measures"
>
</RelativeLayout>
我知道它看起来像一个FrameLayout,但不要被其他人愚弄,它实际上是一个SurfaceView。只需在长时间单击frame时更改SurfaceView的布局参数即可。无需再次设置ContentView
LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
surfaceView.setLayoutParams(lp);
不知何故,请注意,这无助于解决我的问题SetContentView()在onCreate()返回后才具有视觉效果。如果你调用它两次,结果是用户只会看到第二次。我理解,但现在我得到了一个ClassCast异常。我想这是因为我的FrameLayout已经设置为Fill Parent,但是由于我在与这些LayoutParams相对的layout中使用它,所以它无法发布布局文件,因此我可以向您建议出现错误的原因。