如何更改SurfaceView的宽度和高度?

时间:2018-04-06 05:35:54

标签: java android surfaceview

我在ConstraintLayout中有一个SurfaceView。
我改变了SurfaceView的宽度和高度,但只更改了高度。

如何更改SurfaceView宽度?

这是我的activity.xml。

    <android.support.constraint.ConstraintLayout
      xmlns:android = "http://schemas.android.com/apk/res/android"
      xmlns:tools = "http://schemas.android.com/tools"
      android:layout_width = "match_parent"
      android:layout_height = "match_parent"
      xmlns:app = "http://schemas.android.com/apk/res-auto"
      tools:context = ".MainActivity"
    >
    <SurfaceView
        android:id="@+id/surface_play"
        android:layout_width = "0dp"
        android:layout_height = "200dp"
        app:layout_constraintTop_toTopOf ="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"        />

    <Button
        android:id="@+id/btn_play"
        android:layout_width = "wrap_content"
        android:layout_height = "wrap_content"
        android:text="play"
        app:layout_constraintTop_toBottomOf="@+id/surface_play"
        app:layout_constraintStart_toStartOf="parent"        />

    <Button
        android:id="@+id/btn_stop"
        android:layout_width = "wrap_content"
        android:layout_height = "wrap_content"
        app:layout_constraintTop_toTopOf="@+id/btn_play"
        app:layout_constraintStart_toEndOf="@+id/btn_play"        
        android:text="stop"        />    
</android.support.constraint.ConstraintLayout>

这是surfaceChangedMethod。

     @Override
    public void surfaceChanged ( SurfaceHolder holder , int format , int width , 
    int height ) {

    android.view.ViewGroup.LayoutParams lp = surfaceView.getLayoutParams();
    lp.width = 1000; // required width
    lp.height = 1000; // required height
    surfaceView.setLayoutParams(lp);
}

1 个答案:

答案 0 :(得分:0)

我解决了自己。更改SurfaceView的属性

android:layout_width = "0dp"

android:layout_width = "wrap_content" 

比它有效。