android网格布局实现 [英] android gridlayout implementation

查看:80
本文介绍了android网格布局实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

解决方案

您可能想看看 http://developer.android.com/design/building-blocks/grid-lists.html.不完全确定您要做什么,但我希望这会有所帮助.它使用 GridView 而不是 GridLayout.可以在 Android 应用程序中的 GridView VS GridLayout>

这是我的例子

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

这是我的布局

<文本视图android:layout_width="match_parent"android:layout_height="wrap_content"android:background="@android:color/white"android:textColor="@android:color/black"android:textStyle="粗体"android:textSize="50sp"机器人:重力=center_horizo​​ntal"android:text="页面标题"/><!-- 如果需要,插入某种滚动条--><线性布局android:layout_width="match_parent"android:layout_height="wrap_content"机器人:方向=垂直"><文本视图android:layout_width="match_parent"android:layout_height="wrap_content"android:background="@android:color/black"android:textColor="@android:color/white"android:textSize="25sp"机器人:重力=center_horizo​​ntal"android:text="Grid 1 Title"/><网格视图android:id="@+id/gridView1"android:layout_width="fill_parent"android:layout_height="100dp"android:columnWidth="50dp"android:numColumns="auto_fit"机器人:垂直间距=5dp"机器人:水平间距=5dp"android:stretchMode="columnWidth"机器人:重力=中心"/><文本视图android:layout_width="match_parent"android:layout_height="wrap_content"android:background="@android:color/black"android:textColor="@android:color/white"android:textSize="25sp"机器人:重力=center_horizo​​ntal"android:text="Grid 2 Title"/><网格视图android:id="@+id/gridView2"android:layout_width="fill_parent"android:layout_height="100dp"android:columnWidth="50dp"android:numColumns="auto_fit"机器人:垂直间距=5dp"机器人:水平间距=5dp"android:stretchMode="columnWidth"机器人:重力=中心"/></线性布局></LinearLayout>

这是我的主要活动:

package com.example.gridviewdemo;导入 android.app.Activity;导入 android.os.Bundle;导入 android.view.View;导入 android.widget.AdapterView;导入 android.widget.AdapterView.OnItemClickListener;导入 android.widget.GridView;导入 android.widget.Toast;公共类 GridViewExample 扩展活动 {@覆盖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();}});}}

产生:看起来布局在平板电脑上有点偏离,但我相信可以通过一些工作来修复

I'm trying to develop one new project. My client wants me to design like current GooglePlay design (block design per attach). That's why I'm confused whether <android.support.v7.widget.GridLayout> will be useful or not for implement such block design featured. If not, please let me know how to do it?

解决方案

You might want to take a look at http://developer.android.com/design/building-blocks/grid-lists.html. Not exactly sure what you are trying to do, but I hope this helps. It uses GridView instead of GridLayout. A good post about that can be found GridView VS GridLayout in Android Apps

Edit: Here is my example

Note: Most of this is taken from http://developer.android.com/guide/topics/ui/layout/gridview.html. The ImageAdapter used in GridViewExample is taken directly from there as are the pictures used.

Here is my layout

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

Here is my Main Activity:

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();
            }
        });
    }
}

Produces: It looks like the layout is a little off on a tablet, but with some work I am sure that can be fixed

这篇关于android网格布局实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆