【问题标题】:setContentView in OnLongClick ListenerOnLongClick 监听器中的 setContentView
【发布时间】:2012-07-09 09:25:00
【问题描述】:

我遇到了 setContentView 的问题,它会抛出一个空指针。

在我的 mainFrame 中,我有一个小的 SurfaceView,它占据了大约 50% 的屏幕。 如果您长时间单击它,我想实现一种方法,使 SurfaceView 进入全屏状态。我是这样做的:

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。

【问题讨论】:

标签: android layout onlongclicklistener


【解决方案1】:

只需改变SurfaceView onLong click frame的布局参数即可。无需再次setContentView。

  LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);

  surfaceView.setLayoutParams(lp);

【讨论】:

  • 我明白,但我现在得到一个 ClassCast 异常。我认为这是因为我的 FrameLayout 已经设置在 Fill Parent 但是因为我在具有那些 LayoutParams 的 RelativeLayout 中使用它,所以它不会工作
  • 发布您的布局文件,以便我向您提出错误的原因。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-07-01
  • 1970-01-01
  • 2014-06-02
  • 2011-10-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多