在一些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;
實驗結果:看起來更加順眼了,但是仍然存在上下滑動問題。雖然組件大小更加適合設備屏幕,但很顯然這不是主要問題。