stretchMode属性值的作用是设置GridView中的条目以什么缩放模式去填充剩余空间。参数stretchMode 可选值为:none,spacingWidth,columnWidth, spacingWidthUniform

注意:spaceWidth和spacingWidthUniform是有差别的,下面通过一个例子说明一下,本人手机屏幕4.7英寸,分辨率为1280×720

1.建立一个Android项目

界面布局文件activity_main.xml如下:

<?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" >
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="test"
/> <GridView
android:id="@+id/gridview1"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:numColumns="3"
android:columnWidth="80dp"
android:gravity="center"
android:horizontalSpacing="8dp"
android:verticalSpacing="8dp"
android:stretchMode="none"
/> </LinearLayout>

字符串文件strings.xml如下:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">gridviewtest</string>
<string name="c1">F00</string>
<string name="c2">0F0</string>
<string name="c3">00F</string>
<string name="c4">FF0</string>
<string name="c5">F0F</string>
<string name="c6">0FF</string>
<string name="c7">07F</string>
<string name="c8">F07</string>
<string name="c9">70F</string>
</resources>

颜色文件colors.xml如下:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="c1">#F00</color>
<color name="c2">#0F0</color>
<color name="c3">#00F</color>
<color name="c4">#FF0</color>
<color name="c5">#F0F</color>
<color name="c6">#0FF</color>
<color name="c7">#07F</color>
<color name="c8">#F07</color>
<color name="c9">#70F</color>
</resources>

2.编写代码,如下:

public class MainActivity extends Activity {

    int []colors=new int[]
{
R.color.c1,R.color.c2,R.color.c3,
R.color.c4,R.color.c5,R.color.c6,
R.color.c7,R.color.c8,R.color.c9
};
int []texts=new int[]
{
R.string.c1,R.string.c2,R.string.c3,
R.string.c4,R.string.c5,R.string.c6,
R.string.c7,R.string.c8,R.string.c9
};
Button button1;
GridView gridview1; protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); button1=(Button)findViewById(R.id.button1);
gridview1=(GridView)findViewById(R.id.gridview1); button1.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
int width=gridview1.getColumnWidth();
int widthSpace=gridview1.getHorizontalSpacing();
Toast.makeText(MainActivity.this, "columnWidth:"+width+",widthSpace:"+widthSpace, Toast.LENGTH_LONG).show();
} }); final BaseAdapter baseAdapter=new BaseAdapter()
{ @Override
public int getCount() {
return texts.length;
} @Override
public Object getItem(int arg0) {
return getResources().getString(texts[arg0]);
} @Override
public long getItemId(int arg0) {
return arg0;
} @Override
public View getView(int position, View view, ViewGroup viewGroup) { TextView textView=new TextView(MainActivity.this);
textView.setText(getItem(position).toString());
textView.setTextSize(20);
textView.setGravity(Gravity.CENTER);
textView.setBackgroundResource(colors[position]);
textView.setWidth(60);
textView.setHeight(60);
return textView; } }; gridview1.setAdapter(baseAdapter); }
}

3.测试

当将界面布局文件中GridView的stretchMode设为none,点击按钮,输出的信息为columnWidth:160,widthSpace:16

当将界面布局文件中GridView的stretchMode设为spacingWidth,点击按钮,输出的信息为columnWidth:160,widthSpace:120

当将界面布局文件中GridView的stretchMode设为columnWidth,点击按钮,输出的信息为columnWidth:229,widthSpace:16

当将界面布局文件中GridView的stretchMode设为spacingWidthUniform,点击按钮,输出的信息为columnWidth:160,widthSpace:68

GridView的stretchMode属性的更多相关文章

  1. asp.net中GridView的CheckedUnBindCheckBox属性

    1. 获取GridView中CheckBox所选行的字段,即使是在绑定了数据源的时候,也可以获取选中的CheckedUnBindCheckBox对应的各个列的字段 使用时根据实际情况适当的修改即可. ...

  2. Gridview利用DataFormatString属性设置数据格式

    首 先把Gridview的AutoGenerateColumns属性设为False(默认是False),DataField选择相应的字段,特别需要注 意的是要把需要设置的字段的HtmlEncode属性 ...

  3. Gridview的stretchMode等属性详解

    <GridView android:id="@+id/grid"android:layout_width="fill_parent"android:lay ...

  4. GridView的各种属性

    <GridView android:id="@+id/movie_list" android:layout_width="906dp" android:l ...

  5. 解决类型“System.Web.UI.UpdatePanel”不具有名为“Gridview”的公共属性,

    类型“system.web.ui.updatepanel” 不具有名为“XXX”的公共属性,其实原因很简单.就是少了一个<ContentTemplate></ContentTempl ...

  6. Gridview的stretchMode详解附自动宽度

    <GridView android:id="@+id/grid" android:layout_width="fill_parent" android:l ...

  7. android开发之gridView的一些属性。(项目经验总结)

    1.android:numColumns="auto_fit"   //GridView的列数设置为自动 2.android:columnWidth="90dp &quo ...

  8. android中GridView关于间距的属性值介绍

    android:columnWidth  设置列的宽度.关联的方法为:setColumnWidth(int)  stretchMode属性值的作用是设置GridView中的条目以什么缩放模式去填充空间 ...

  9. C#中gridView常用属性和技巧介绍

    .隐藏最上面的GroupPanel gridView1.OptionsView.ShowGroupPanel=false; .得到当前选定记录某字段的值 sValue=Table.Rows[gridV ...

随机推荐

  1. Delegate与Event关系

    1.Delegate是类型,Event是成员(本质为成员方法),Event成员类型派生于Delegate.仅此! 2.大概就好比说委托是C++里一个方法的模板,而event是这个模板具体的实现

  2. Nginx不支持Post数据中带有"."

    如果提交的数据中带有.,nginx会把数据过滤掉,会导致后台收不到数据.下面是一个错误示例及解决办法

  3. ASP.NET MVC 3 Razor 语法

    1.   三元运算符 1)   输出文本 1.   View var var1 = '@(1 < 2 ? "YES" : "NO")'; var var2 ...

  4. cygwin 安装包管理器 apt-cyg

    https://github.com/transcode-open/apt-cyg apt-cyg is a simple script. To install: lynx -source https ...

  5. Eclipse 自动生成 Ant的Build.xml 配置文件

    Eclipse 自动生成 Ant的Build.xml 配置文件,生成的方法很隐蔽 选择你要生成Build.xml文件的项目,右键. Export-> General -> Ant Buil ...

  6. UOJ #36「清华集训2014」玛里苟斯

    这怎么想得到啊......... UOJ #36 题意:求随机一个集合的子集的异或和的$k$次方的期望值,保证答案$ \lt 2^{63},1 \leq k \leq 5$ $ Solution:$ ...

  7. 使用IDEA新建Web工程启动报404的错误

    新换了一个项目组被人吐槽配置文件都能写错,所以打算从头开始一步步搭建一个项目,包含ssm基础框架.mongodb工具类.redis工具类.jsf配置.log配置等 今天先来搭建一个web工程.工程搭建 ...

  8. Java内存模型-final域的内存语义--没明白,预留以后继续理解

    https://www.cnblogs.com/yuanfy008/p/9349275.html 来自 Java并发编程(1)-Java内存模型

  9. 遗传算法selection总结-[Fitness, Tournament, Rank Selection]

    假设个体(individual)用\(h_i\)表示,该个体的适应度(fitness)为\(Fitness(h_i)\),被选择的概率为\(P(h_i)\). 另外假设种群(population)的个 ...

  10. Angular 创建组件

    创建组件 0 命令创建 1 创建组件 定义hello.component.ts组件 在app.module.ts中引用并添加到declarations声明中 在app.component.html中使 ...