如何将我的 linearLayout 转换为 GridView/GridLayout

How to convert my linearLayout to GridView/GridLayout

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

示例工程已上传here.

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

更新: 这是我在 class:

中的 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);
        }
    }

试试这个例子,看看它是如何为你工作的。

<GridLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/grid_layout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:columnCount="3"
    android:rowCount="2"
    android:orientation="horizontal">

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

</GridLayout>

GridLayout 可能无法工作,因为它的内部实现(a set of infinitely thin lines that separate the viewing area into cells). Try using a container designed to display views efficiently such as a RecyclerView or its new cousins, such as the HorizontalGridView or the VerticalGridView, you can find a set of useful examples for the latter here

概念是为行创建一个linearLayout,为列创建另一个linearLayout来添加它。在 onCreate 方法中添加此代码

    int column = 3;
    int row = 4;
    RelativeLayout relativeLayout = (RelativeLayout) findViewById(Your  RelativeLayout_in_main_activity_id);
    LinearLayout rowLayout = new LinearLayout(this);
    rowLayout.setOrientation(LinearLayout.VERTICAL);
    relativeLayout.addView(rowLayout);
    for (int i = 0; i<row;i++) {
        LinearLayout columnLayout = new LinearLayout(this);
        columnLayout.setOrientation(LinearLayout.HORIZONTAL);
        for (int j = 0; j<column;j++) {
            TextView textView = new TextView(this);
            columnLayout.addView(textView ); 
        }
        rowLayout.addView(columnLayout);
    }

希望这个答案是你想要的。

请这样检查。首先在 MainActivity 或任何你想要的地方定义你的网格视图。然后做一个custom_layout

GridViewActivity class

 <GridView
            android:id="@+id/gridView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:clickable="true"
            android:columnWidth="170dp"
            android:drawSelectorOnTop="true"
            android:focusable="true"
            android:gravity="center"
            android:horizontalSpacing="5dp"
            android:numColumns="auto_fit"
            android:stretchMode="columnWidth"
            android:verticalSpacing="5dp" />

现在创建布局。 在该布局中,您可以放置​​图像视图或 表面视图 任何您想要的东西

  <SurfaceView
    android:id="@+id/video_1_surfaceview"
    android:layout_width="170dp"
    android:layout_height="130dp"
    />

只要一个 就够了

现在创建一个 AdapterClass 并扩充您的自定义布局。就是这样 如果您还有任何问题,请告诉我。

<GridLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/grid_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:columnCount="3"
android:rowCount="2">

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

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

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

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

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

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

对我有用的最简单方法是使用嵌套 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>