(android控件)界面布局实现GridView(网格效果)

1 需要实现的效果

处于业务的需求,需要实现网格效果,具体来讲 屏幕中一行等分两块,每一块中居中放一个图片。

补充要求:考虑界面设计时,通用于不同分辨率手机屏幕,则不能通过固定位置布局

 

实现界面效果图:

 

2 神奇的控件属性:layout_weight

layout_weight 属性

属性的意义: 设置额外分配给控件的空间,默认为0 ,控件保持不变 ,属性大于0,则将屏幕剩余的控件分配给控件。

 

3 解决思路

     [一般的实现的方式]

             设置一个横向布局,放置控件,并设置控件之间的间隔,但对于不同尺寸的屏幕,要做多套界面

 

    [本文的解决思路]

           设置一个横向布局,在布局中添加数个 线性布局,并统一设置属性 layout_weight =1,居中 ,在线性布局中放入需要的控件,这样利用layout_weight属性让线性布局等分,且可以保证需要的控件不走样。

 

 

4 实战实现代码

 

   <LinearLayout
android:id="@+id/linearLayout1"
android:layout_width
="fill_parent"
android:orientation
="horizontal"
android:layout_marginTop
="15dp"
android:layout_height
="wrap_content" >

<LinearLayout
android:id="@+id/linearLayout_n1"
android:layout_width
="fill_parent"
android:orientation
="horizontal"
android:layout_weight
="1"
android:gravity
="center"
android:layout_height
="wrap_content" >
<ImageView
android:id="@+id/img_1"
android:adjustViewBounds
="true"
android:layout_width
="wrap_content"
android:layout_height
="wrap_content"
android:padding
="4dp"
android:focusable
="true"
android:clickable
="true"
android:tag
="1"
android:background
="@drawable/imgselect"
android:src
="@drawable/sl1_1" />
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayout_n2"
android:layout_width
="fill_parent"
android:layout_weight
="1"
android:orientation
="horizontal"

android:gravity
="center"
android:layout_height
="wrap_content" >
<ImageView
android:id="@+id/img_2"
android:adjustViewBounds
="true"
android:layout_width
="wrap_content"
android:layout_height
="wrap_content"
android:padding
="4dp"
android:tag
="2"
android:focusable
="true"
android:clickable
="true"
android:background
="@drawable/imgselect"
android:src
="@drawable/sl1_2" />
</LinearLayout>
</LinearLayout>

 

           

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值