平板上网格列表的实现
在平板上,很多app里面都有类似于电脑的那种列表,可以上下滑动,之后每一列有很多类别,每一个类别占用空间不同,实现起来有些复杂,下面直接上图
这里中间还没有分割线,如果有分割线,可能更复杂。而且Android平板尺寸多,需要做到适配,所以不能写固定尺寸。
想了好久,也就明白了,实现起来也很简单,只需要ListView或者RecycleView实现上下滑动,之后item中采用线性布局,使用权重来分配空间。可能你会说权重怎么计算,这个复杂,不好算呀,其实不然。下面就说说权重的使用。
一般ui都会给一个标注图,如果标注图上有标注每一个单元个宽度,那就好办了,直接将标注的宽度当做权重来使用,这样系统会自动计算宽度,可能权重看起来会很大,很奇怪,可是这样是最简单的,不是吗。
如果没有标注,那你就拖一张设计图到ps中,量一下每一个的宽度,同样的方法,直接就搞定了,而且适配也完成了,是不是很轻松。下面贴一下示例布局:
<?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="51dp"
xmlns:tools="http://schemas.android.com/tools"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:id="@+id/tv_orderId"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="296"
android:gravity="center"
android:singleLine="true"
tools:text="JRFR45"
android:textColor="@color/grey_484848"
android:textSize="14sp"
android:maxLines="1"
android:lines="1"/>
<TextView
android:id="@+id/tv_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="296"
android:gravity="center"
android:singleLine="true"
tools:text="刘晓忠"
android:textColor="@color/grey_484848"
android:textSize="14sp"
android:maxLines="1"
android:lines="1"/>
<TextView
android:id="@+id/tv_tel"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="296"
android:gravity="center"
android:singleLine="true"
tools:text="13800000000"
android:textColor="@color/grey_484848"
android:textSize="14sp"
android:maxLines="1"
android:lines="1"/>
<TextView
android:id="@+id/tv_price"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="296"
android:gravity="center"
android:singleLine="true"
tools:text="1634"
android:textColor="@color/grey_484848"
android:textSize="14sp"
android:maxLines="1"
android:lines="1"/>
<TextView
android:id="@+id/tv_status"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="296"
android:gravity="center"
android:singleLine="true"
tools:text="未受理"
android:textColor="@color/guide_red"
android:textSize="14sp"
android:maxLines="1"
android:lines="1"/>
<TextView
android:id="@+id/tv_time"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="300"
android:gravity="center"
android:singleLine="true"
tools:text="2017 - 01 - 07 16:24:27"
android:textColor="@color/grey_484848"
android:textSize="14sp"
android:maxLines="1"
android:lines="1"/>
<TextView
android:id="@+id/tv_manager"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="268"
android:gravity="center"
android:singleLine="true"
tools:text="1984"
android:textColor="@color/grey_484848"
android:textSize="14sp"
android:maxLines="1"
android:lines="1"/>
</LinearLayout>
在as上的预览效果如下:
加油!