思路:

GridView行数设置为一行,外面套一个HorizontalScrollView,代码中设置GridView宽度

xml代码

<HorizontalScrollView
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:scrollbars="none" > <LinearLayout
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:orientation="horizontal" >   <GridView
     android:id="@+id/gv"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >
  </GridView>
</LinearLayout>
</HorizontalScrollView>

代码中设置

    // 得到像素密度
DisplayMetrics outMetrics = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay()
.getMetrics(outMetrics);
float density = outMetrics.density; // 像素密度
ViewGroup.LayoutParams params = gridView.getLayoutParams();
int itemWidth = (int) (100 * density);// 默认item宽度为100
int spacingWidth = (int) (5 * density);
params.width = (itemWidth + spacingWidth) * childCount + spacingWidth;
gridView.setStretchMode(GridView.NO_STRETCH); // 设置为禁止拉伸模式
gridView.setNumColumns(childCount);
gridView.setHorizontalSpacing(spacingWidth);
gridView.setColumnWidth(itemWidth);
gridView.setLayoutParams(params);

横向滑动的GridView的更多相关文章

  1. 使GridView可以单行横向滑动

    最近做的练手的小项目中存在一个横向滑动的问题,需要HorizontalScroll中嵌套GridView,但是GridView默认是竖直排放的item,况且HorizontalScroll与GridV ...

  2. 横向滑动的HorizontalListView滑动指定位置的解决方法

    项目中用到了自定义横向滑动的控件:HorizontalListView,点击其中一项,跳转到另外一个大图界面,大图界面也是HorizontalListView,想使用setSelection方法设定 ...

  3. UICollectionView 图片横向滑动居中偏移量的解决

    1.在使用UICollectionView 来显示横向滑动图片的时候,cell与cell之间有间隙,每次滑动后cell都会向左偏移,在使用过这两个方法才解决每次向左偏移的部分. 2.使用方法前不要开启 ...

  4. ios7中使用scrollview来横向滑动图片,自动产生偏移竖向的偏移 问题

    ios7中使用scrollview来横向滑动图片,自动产生偏移竖向的偏移 问题     如图红色为scrollview的背景色,在scrollview上加了图片之后,总会有向下的偏移 设置conten ...

  5. android--解--它们的定义tabhost(动态添加的选项+用自己的主动性横向滑动标签+手势切换标签页和内容特征)

    在本文中,解决他们自己的定义tabhost实现,并通过代码集成动态加入标签功能.自己主动标签横向滑动功能.和手势标签按功能之间切换. 我完成了这个完美的解决方案一起以下: 1.定义tabwidget布 ...

  6. overflow-x: scroll;横向滑动详细讲解

    overflow-x: scroll;横向滑动(移动端使用详解) css3 , ie8以上 <!DOCTYPE html> <html lang="en"> ...

  7. UITableView 的横向滑动实现

    UITableView 的横向滑动实现 概述 为了实现横向滑动的控件,可以继承类 UIScrollView 或类 UIView 自定义可以横向滑动的控件,这里通过 UITableView 的旋转,实现 ...

  8. 用css巧妙实现移动端横向滑动展示功能

    前言:记得以前处理移动端横向滑动展示都是去用js去解决的,要用js进行蛮多处理,要算li的宽度,然后还要用js设置ul盒子的宽度,又要设置最大滑动距离,最小滑动距离等等.......但是现在发现用cs ...

  9. iOS8 UICollectionView横向滑动demo

    在iOS8中,scrollView和加载在它上面的点击事件会有冲突,所以做一个横向滑动的界面最好的选择就是UICollectionView. 这个效果可以用苹果公司提供的官方demo修改而来,下载地址 ...

随机推荐

  1. ECMAScript数组常用

    var arr = [22, 33, 44, 55, 66, 77, 88, 99]; //every 全部结果为true 则返回true var e = arr.every(function (m) ...

  2. JSONObject,JSONArray,Map,String之间转换

    http://blog.csdn.net/superit401/article/details/51727739 1.String转JSONObject String jsonMessage = &q ...

  3. Mysql主从同步遇到的一些问题

    为提供更快的访问速度,在不同的地区增加了一台只供访问的从服务器.因此要将主服务器的数据全部备份过去,并且设置主从同步 假设: 主服务器IP:192.168.1.10 从服务器IP:192.168.1. ...

  4. Latex 建立带有竖线和编号的算法环境

    Latex 建立带有竖线和编号的算法环境 Latex源码: \documentclass{article} \usepackage{amssymb} \usepackage{amsmath} \use ...

  5. F:ungeon Master

    总时间限制: 1000ms 内存限制: 65536kB描述You are trapped in a 3D dungeon and need to find the quickest way out! ...

  6. Eclipse 安装需要的 JDK 版本简要说明

    Eclipse 4.6 (Neon) Eclipse 4.6 (Neon)is scheduled for release on June 22, 2016. Consider using the I ...

  7. UISegmentControl

    @import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/c ...

  8. Python模块学习笔记

    1.作用域 私有private:用'_x'或'__xx'表示,如,_a,__ab; 公有public: 如 a,b; 特殊变量,可被直接引用,如:__author__,__name__,命名变量时一般 ...

  9. ComboxEdit实现二级联动

    private void cmbXPROVINCE_SelectedIndexChanged(object sender, EventArgs e) { DataBind(cmbXPROVINCE, ...

  10. 工具 cocoapods和插件

    命令行更新(安装)步骤 $ sudo gem update --system // 先更新gem,国内需要切换源 $ gem sources --remove https://rubygems.org ...