在一些APP上我们经常看见这样子的布局:
实现的思路有两种:
1 最普通的是直接在XML设置,利用LinearLayout嵌套LinearLayout,然后设置各个控件android:layout_weight属性实现空间分割
2 利用网格视图GridView
一、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" >
<LinearLayout
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:background="@drawable/bg_border"
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="1" />
<Button
android:background="@drawable/bg_border"
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="2" />
</LinearLayout>
<LinearLayout
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:background="@drawable/bg_border"
android:id="@+id/button3"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="3" />
<Button
android:background="@drawable/bg_border"
android:layout_weight="1"
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="4" />
</LinearLayout>
<LinearLayout
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/bg_border"
android:text="5" />
<Button
android:background="@drawable/bg_border"
android:layout_weight="1"
android:id="@+id/button6"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="6" />
</LinearLayout>
</LinearLayout>
二、网格视图GridView
package com.example.tail;
import java.util.ArrayList;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
public class qq extends Activity {
private int[] imageID ={R.drawable.icon1,R.drawable.icon2,R.drawable.icon3,R.drawable.icon4,R.drawable.icon5,R.drawable.icon6,R.drawable.icon7,R.drawable.icon8};
private String[] titles = {"今日看点","新浪微博","我的收藏","新闻头条","科技频道","汽车频道","军事频道","新华炫文"};
ArrayList<imageInfo> m;//封装了保存图像ID与文本标题的Item对象
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//加载GridView对象并获取设备屏幕高度
setContentView(R.layout.layout_gv);
DisplayMetrics dm = getResources().getDisplayMetrics();
final int ScreenHight = dm.heightPixels;
//实例化数据
m =imageInfo.get(imageID,titles);
GridView g = (GridView)findViewById(R.id.gridView1);
g.setAdapter(new BaseAdapter() {
public View getView(int position, View convertView, ViewGroup parent) {
//加载子选项界面并布局
LinearLayout item =(LinearLayout)LayoutInflater.from(qq.this).inflate(R.layout.grid_item, null);
ImageView iv = (ImageView)item.findViewById(R.id.imageView1);
TextView tv = (TextView)item.findViewById(R.id.textView1);
iv.setBackgroundResource(m.get(position).imageid);
tv.setText(m.get(position).title);
item.setLayoutParams(new AbsListView.LayoutParams(
android.view.ViewGroup.LayoutParams.MATCH_PARENT,
ScreenHight/4));
return item;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return 8;
}
});
}
}
可以看出,实际效果与我们所需的不同:我们是要实现不能滑动,完美填充屏幕的等格子布局,很明显组件内容超出了屏幕最大高度。在getView办法中添加断点测试34与45行,在Debug模式中发现作为父容器的GridView的高度只有762,作为GridView父容器的FrameLayout高度也是762,但是再上一级的父容器LinearLayout容器就有800了。然后我们再观察GridView里的mChlidren里的每个Item组件的高度确实是200。所以很有可能我们得到的屏幕高度是手机固定的分辨率,包括可分配空间与不可分配空间(例如通知栏的高度也算进去了)。那么很自然而然我们设每个组件的高度为设备的高度的1/N必然会溢出。
int ScreenHight = g.getHeight()/4;
实验结果:看起来更加顺眼了,但是仍然存在上下滑动问题。虽然组件大小更加适合设备屏幕,但很显然这不是主要问题。