如何将linearLayout转换为GridView/GridLayout

Tin*_*a J 4 android gridview android-linearlayout android-gridlayout

如何将如下所示的线性布局转换为网格视图?所以基本上我需要它是3列和2行(横向).也没有国界.

示例项目在此处上传.

在此输入图像描述

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

        <SurfaceView
            android:id="@+id/video_1_surfaceview"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1" />

        <SurfaceView
            android:id="@+id/video_2_surfaceview"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1" />

        <SurfaceView
            android:id="@+id/video_3_surfaceview"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1" />

        <SurfaceView
            android:id="@+id/video_4_surfaceview"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1" />

        <SurfaceView
            android:id="@+id/video_5_surfaceview"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1" />

        <SurfaceView
            android:id="@+id/video_6_surfaceview"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1" />

    </LinearLayout>
Run Code Online (Sandbox Code Playgroud)

更新:这是我的onCreate()方法和类中的初始化:

public class MultipleVideoPlayActivity extends Activity implements OnBufferingUpdateListener, OnCompletionListener, OnPreparedListener, OnVideoSizeChangedListener, SurfaceHolder.Callback {

    private static final String TAG = "MediaPlayer";
    private static final int[] SURFACE_RES_IDS = { R.id.video_1_surfaceview, R.id.video_2_surfaceview };

    private MediaPlayer[] mMediaPlayers = new MediaPlayer[SURFACE_RES_IDS.length];
    private SurfaceView[] mSurfaceViews = new SurfaceView[SURFACE_RES_IDS.length];
    private SurfaceHolder[] mSurfaceHolders = new SurfaceHolder[SURFACE_RES_IDS.length];
    private boolean[] mSizeKnown = new boolean[SURFACE_RES_IDS.length];
    private boolean[] mVideoReady = new boolean[SURFACE_RES_IDS.length];
    int i = 0; // index of video playout

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
         setContentView(R.layout.multi_videos_layout);

        // create surface holders
        for (int i = 0; i < mSurfaceViews.length; i++) {
            mSurfaceViews[i] = (SurfaceView) findViewById(SURFACE_RES_IDS[i]);
            mSurfaceHolders[i] = mSurfaceViews[i].getHolder();
            mSurfaceHolders[i].addCallback(this);
            mSurfaceHolders[i].setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        }
    }
Run Code Online (Sandbox Code Playgroud)

Tin*_*a J 5

对我来说最简单的方法是使用嵌套linearLayout,如下(2x2).我现在唯一的问题是我不知道如何设置水平布局的高度恰好是屏幕高度的一半!

任何人对此都有任何想法?

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

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <SurfaceView
            android:id="@+id/video_1_surfaceview"
            android:layout_width="wrap_content"
            android:layout_height="150dp"
            android:layout_weight="1" />

        <SurfaceView
            android:id="@+id/video_2_surfaceview"
            android:layout_width="wrap_content"
            android:layout_height="150dp"
            android:layout_weight="1" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <SurfaceView
            android:id="@+id/video_3_surfaceview"
            android:layout_width="wrap_content"
            android:layout_height="150dp"
            android:layout_weight="1" />

        <SurfaceView
            android:id="@+id/video_4_surfaceview"
            android:layout_width="wrap_content"
            android:layout_height="150dp"
            android:layout_weight="1" />
    </LinearLayout>

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)