1、布局文件如下
文件名:testxm.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:background="#f1f1f1"
- android:orientation="vertical" >
- <!-- 顶部蓝色工具栏 -->
- <RelativeLayout
- android:id="@+id/fujin_top_llayout"
- android:layout_width="fill_parent"
- android:layout_height="36dp"
- android:background="#47a9d0"
- android:gravity="center_vertical"
- android:orientation="horizontal" >
- <TextView
- android:id="@+id/fujin_top_llayout_lessthan"
- android:layout_width="wrap_content"
- android:layout_height="30dp"
- android:layout_alignParentLeft="true"
- android:layout_marginLeft="10dip"
- android:paddingLeft="10dp"
- android:text="1111" />
- <TextView
- android:id="@+id/fujin_top_llayout_fujin"
- android:layout_width="wrap_content"
- android:layout_height="30dp"
- android:layout_toRightOf="@+id/fujin_top_llayout_lessthan"
- android:paddingLeft="10dp"
- android:paddingTop="3dp"
- android:text="222"
- android:textColor="#fff"
- android:textSize="16sp" />
- <TextView
- android:id="@+id/fujin_top_llayout_dingwei"
- android:layout_width="70dp"
- android:layout_height="30dp"
- android:layout_toLeftOf="@+id/fujin_top_llayout_search"
- android:text="33333" />
- <TextView
- android:id="@+id/fujin_top_llayout_search"
- android:layout_width="wrap_content"
- android:layout_height="30dp"
- android:layout_alignParentRight="true"
- android:paddingRight="12dp"
- android:text="44444"/>
- </RelativeLayout>
- <!-- 顶部蓝色工具栏结束 -->
- <ScrollView
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:fillViewport="true"
- android:layout_weight="1"
- android:scrollbars="vertical" >
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:orientation="vertical" >
- <!-- title -->
- <RelativeLayout
- android:id="@+id/fenlei_title_rl"
- android:layout_width="match_parent"
- android:layout_height="28dp"
- android:layout_marginLeft="15dp"
- android:layout_marginRight="15dp"
- android:layout_marginTop="20dp" >
- <TextView
- android:id="@+id/fenlei_title_tv"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:gravity="center"
- android:text="标题"
- android:textSize="15sp" />
- </RelativeLayout>
- <!-- title_over -->
- <!-- btnlist -->
- <LinearLayout
- android:id="@+id/fujin_btnlist_tl"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_marginTop="15dp"
- android:orientation="vertical"
- android:scrollbarAlwaysDrawVerticalTrack="true" >
- </LinearLayout>
- <!-- btnlist_over -->
- </LinearLayout>
- </ScrollView>
- </LinearLayout>
2、gridview的item布局文件
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_height="wrap_content"
- android:paddingBottom="4dip" android:layout_width="fill_parent">
- <TextView
- android:layout_width="wrap_content"
- android:layout_below="@+id/ItemImage"
- android:layout_height="wrap_content"
- android:text="TextView01"
- android:layout_centerHorizontal="true"
- android:id="@+id/ItemText">
- </TextView>
- </RelativeLayout>
3、activity代码
- public class PorterDuffActivity extends Activity{
- private PorterCanvas mPorterCanvas = null;
- private GridView gridView;
- private LinearLayout ll;
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.testxm);
- this.addview();
- }
- private void addview(){
- for(int i=1; i<=35;i++){
- ll=(LinearLayout) findViewById(R.id.fujin_btnlist_tl);
- this.addTwoClassify(i);
- }
- }
- private void makeGridView(int num){
- ArrayList<HashMap<String, Object>> lstImageItem = new ArrayList<HashMap<String, Object>>();
- for(int i=0;i<10;i++)
- {
- HashMap<String, Object> map = new HashMap<String, Object>();
- map.put("ItemText", num+"NO."+String.valueOf(i));//按序号做ItemText
- lstImageItem.add(map);
- }
- //生成适配器的ImageItem <====> 动态数组的元素,两者一一对应
- SimpleAdapter saImageItems = new SimpleAdapter(this, //没什么解释
- lstImageItem,//数据来源
- R.layout.gridview_item,//night_item的XML实现
- //动态数组与ImageItem对应的子项
- new String[] {"ItemText"},
- //ImageItem的XML文件里面的一个ImageView,两个TextView ID
- new int[] {R.id.ItemText});
- //添加并且显示
- gridView.setAdapter(saImageItems);
- }
- private void addTwoClassify(int i){
- gridView=new MyGridView(PorterDuffActivity.this);//注意这里使用的是MyGridView,如果使用GridView的话,只会显示一行多一点,第二行显示不完全,使用MyGridView的话,能够完全显示出来。commend by danielinbiti
- gridView.setVerticalSpacing(10);
- gridView.setHorizontalSpacing(15);
- gridView.setNumColumns(3);
- this.makeGridView(i);
- LinearLayout ll_two=new LinearLayout(PorterDuffActivity.this);
- ll_two.setPadding(10, 0, 0, 0);
- ll_two.addView(gridView,
- new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
- ll.addView(ll_two, new LinearLayout.LayoutParams(
- LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
- }
- }
4、MyGridView代码
- public class MyGridView extends GridView{
- private boolean needScrollBar = false; //设置是否有ScrollBar,当要在ScollView中显示时,应当设置为false。 否则为 true
- public MyGridView(Context context) {
- super(context);
- }
- public MyGridView(Context context, AttributeSet attrs) {
- super(context, attrs);
- }
- public MyGridView(Context context, AttributeSet attrs, int defStyle) {
- super(context, attrs, defStyle);
- }
- @Override
- protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
- if (!needScrollBar) {
- int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
- super.onMeasure(widthMeasureSpec, expandSpec);
- } else {
- super.onMeasure(widthMeasureSpec, heightMeasureSpec);
- }
- }
- }
总结:
因为ScrollView和GridView有布局上的冲突,如果外层嵌套了ScrollView,则GridView如果不是指定固定高度,则无法显示完全。ListView也有该问题。
但其实针对这到题来说,我觉得使用LinearLayout等分方式的布局也是可以的,垂直布局往下添加就可以了,这样避免了与ScrollView的冲突。但主要因为GridView是封装好的组件,所以使用了GridView能够简化一部分工作,但也增加了布局刷新的负担(虽然一般可以不考虑)
转自:http://blog.csdn.net/danielinbiti/article/details/45956745