(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>
posted on 2012-02-15 15:34 狗尾草-大数据收割基 阅读(8124) 评论(0) 编辑 收藏 举报