标签:
GridView相信也很常见,但是GridView有一个缺点就是无法控制整个GridView的高度,只能通过子view去控制其高度。
项目中往往会有这样需求,让一个Gridview铺满整个屏幕
关键点:
在GridView的adapter中对item高度进行计算,然后再设置。
核心代码:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView==null){
convertView= LayoutInflater.from(context).inflate(R.layout.item_shmain ,parent ,false);
holder=new ViewHolder();
holder.itemIV= (ImageView) convertView.findViewById(R.id.iv_item);
holder.itemTV= (TextView) convertView.findViewById(R.id.tv_item);
convertView.setTag(holder);
}else {
holder= (ViewHolder) convertView.getTag();
}
holder.itemIV.setBackgroundResource(images[position]);
holder.itemTV.setText(names[position]);
LinearLayout.LayoutParams lp=new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
(CommonUtil.getDeviceHeight(context)-CommonUtil.getBarHeight(context))/3);
convertView.setLayoutParams(lp);
return convertView;
}
这里设置高度是屏幕的高度减掉标题栏的高度,然后除以gridview的行数(这里直接写死 了3).通过动态设置adapter的convertview的高度就实现了GridView填充整个屏幕。
最后贴出所有代码记录:
public class MainActivity extends AppCompatActivity {
private GridView mGridView;
private String[] itemName={"扫码安装" ,"扫码维修" ,"设备数据查询" ,"我的工作记录" ,"关于我们" ,"设置"};
private int[] imgId={R.drawable.icon_sh_1 ,R.drawable.icon_sh_2 ,R.drawable.icon_sh_3 ,
R.drawable.icon_sh_4 ,R.drawable.icon_sh_5 ,R.drawable.icon_sh_6};
private SHGridAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
mGridView= (GridView) findViewById(R.id.gv_sh);
mAdapter=new SHGridAdapter(this,itemName,imgId);
mGridView.setAdapter(mAdapter);
}
public class SHGridAdapter extends BaseAdapter{
private Context context;
private String[] names;
private int[] images;
public SHGridAdapter(Context context, String[] names, int[] images) {
this.context = context;
this.names = names;
this.images = images;
}
@Override
public int getCount() {
return names.length;
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView==null){
convertView= LayoutInflater.from(context).inflate(R.layout.item_shmain ,parent ,false);
holder=new ViewHolder();
holder.itemIV= (ImageView) convertView.findViewById(R.id.iv_item);
holder.itemTV= (TextView) convertView.findViewById(R.id.tv_item);
convertView.setTag(holder);
}else {
holder= (ViewHolder) convertView.getTag();
}
holder.itemIV.setBackgroundResource(images[position]);
holder.itemTV.setText(names[position]);
LinearLayout.LayoutParams lp=new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
(CommonUtil.getDeviceHeight(context)-CommonUtil.getBarHeight(context))/3);
convertView.setLayoutParams(lp);
return convertView;
}
class ViewHolder{
private ImageView itemIV;
private TextView itemTV;
}
}
public class CommonUtil {
public static int getDeviceHeight(Context context){
WindowManager wm= (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
return wm.getDefaultDisplay().getHeight();
}
/**
* 获取状态栏高度
*/
public static int getBarHeight(Context context){
Class<?> c = null;
Object obj = null;
Field field = null;
int x = 0, sbar = 38;//默认为38,貌似大部分是这样的
try {
c = Class.forName("com.android.internal.R$dimen");
obj = c.newInstance();
field = c.getField("status_bar_height");
x = Integer.parseInt(field.get(obj).toString());
sbar = context.getResources().getDimensionPixelSize(x);
} catch (Exception e1) {
e1.printStackTrace();
}
return sbar;
}
}
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center"
android:background="@drawable/bg_gridview"
android:paddingTop="30dp"
android:paddingBottom="30dp"
>
<ImageView
android:id="@+id/iv_item"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_centerHorizontal="true"
android:background="@drawable/icon_sh_1"/>
<TextView
android:id="@+id/tv_item"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="扫码安装"
android:gravity="center_horizontal"
android:textSize="17sp"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
/>
</LinearLayout>
这里每个gridview的边框效果是通过一个背景实现的(其实有缺陷,部分地方有重复)
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape android:shape="rectangle">
<stroke android:width="1.0px"
android:color="#e12121"/>
<gradient android:angle="270.0"
android:endColor="#ffe8ecef"
android:startColor="#ffe8ecef"/>
</shape>
</item>
<item android:state_focused="true">
<shape android:shape="rectangle">
<shape android:shape="rectangle">
<stroke android:width="1.0px"
android:color="#e12121"/>
<gradient android:angle="270.0"
android:endColor="#ffe8ecef"
android:startColor="#ffe8ecef"/>
</shape>
</shape>
</item>
<item>
<shape android:shape="rectangle">
<gradient android:angle="270.0"
android:endColor="#ffffffff"
android:startColor="#ffffffff" />
<stroke android:width="1.0px"
android:color="#e12121" />
</shape>
</item>
</selector>
ok,代码很功能都很基础。感觉有用的朋友可以留言支持,一起进步!
Android GridView无法填充Activity的解决办法
标签:
原文地址:http://blog.csdn.net/u014733374/article/details/51351947