android gridlayout实现

时间:2013-07-15 03:33:38

标签: android responsive-design

我正在尝试开发一个新项目。我的客户希望我设计当前的GooglePlay设计(每个附件的块设计)。这就是为什么我很困惑<android.support.v7.widget.GridLayout>是否对实现这样的块设计有用。如果没有,请告诉我怎么做?

enter image description here

1 个答案:

答案 0 :(得分:5)

您可能需要查看http://developer.android.com/design/building-blocks/grid-lists.html。不完全确定你要做什么,但我希望这会有所帮助。它使用GridView而不是GridLayout。关于这一点的好帖子可以找到GridView VS GridLayout in Android Apps

编辑:这是我的例子

注意: 大部分内容取自http://developer.android.com/guide/topics/ui/layout/gridview.html。 GridViewExample中使用的ImageAdapter直接从那里获取,使用的图片也是如此。

这是我的布局

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

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/white"
        android:textColor="@android:color/black"
        android:textStyle="bold"
        android:textSize="50sp"
        android:gravity="center_horizontal"
        android:text="Page Title" />

    <!-- Insert some sort of scrolling if desired -->

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

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/black"
            android:textColor="@android:color/white"
            android:textSize="25sp"
            android:gravity="center_horizontal"
            android:text="Grid 1 Title" />

        <GridView
            android:id="@+id/gridView1"
            android:layout_width="fill_parent"
            android:layout_height="100dp"
            android:columnWidth="50dp"
            android:numColumns="auto_fit"
            android:verticalSpacing="5dp"
            android:horizontalSpacing="5dp"
            android:stretchMode="columnWidth"
            android:gravity="center" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/black"
            android:textColor="@android:color/white"
            android:textSize="25sp"
            android:gravity="center_horizontal"
            android:text="Grid 2 Title" />

        <GridView
            android:id="@+id/gridView2"
            android:layout_width="fill_parent"
            android:layout_height="100dp"
            android:columnWidth="50dp"
            android:numColumns="auto_fit"
            android:verticalSpacing="5dp"
            android:horizontalSpacing="5dp"
            android:stretchMode="columnWidth"
            android:gravity="center" />         
    </LinearLayout>
</LinearLayout>

这是我的主要活动:

package com.example.gridviewdemo;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.Toast;

public class GridViewExample extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.gridview_example);

        GridView gridView1 = (GridView) findViewById(R.id.gridView1);
        gridView1.setAdapter(new ImageAdapter(this));

        gridView1.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
                Toast.makeText(GridViewExample.this, "1: " + position, Toast.LENGTH_SHORT).show();
            }
        });

        GridView gridView2 = (GridView)findViewById(R.id.gridView2);
        gridView2.setAdapter(new ImageAdapter(this));

        gridView2.setOnItemClickListener(new OnItemClickListener(){
            public void onItemClick(AdapterView<?> parent, View v, int position, long id){
                Toast.makeText(GridViewExample.this, "2: " + position, Toast.LENGTH_SHORT).show();
            }
        });
    }
}

产地: Image 看起来平板电脑的布局有点偏,但有些工作我确信可以修复