u011732740
SaluteWarmTinct
2015-05-23 08:42
采纳率: 86%
浏览 3.6k

Android自动生成了一堆的button列表用,因为屏幕高度问题没显示完全。

图片说明
想要的效果是这样的,也就是可以上下拖动,顶部蓝色layout固定不动。
图片说明
代码如下:


//布局如下:

        <!-- 顶部蓝色工具栏 -->
        <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" >
        <ImageView
            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:src="@drawable/fujin_top_llayout_lessthan" />
        <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="@string/fenlei_top_llayout_fenlei"
            android:textColor="#fff"
            android:textSize="16sp" />
        <ImageView
            android:id="@+id/fujin_top_llayout_dingwei"
            android:layout_width="70dp"
            android:layout_height="30dp"
            android:layout_toLeftOf="@+id/fujin_top_llayout_search"
            android:src="@drawable/iocn2" />
        <ImageView
            android:id="@+id/fujin_top_llayout_search"
            android:layout_width="wrap_content"
            android:layout_height="30dp"
            android:layout_alignParentRight="true"
            android:paddingRight="12dp"
            android:src="@drawable/fujin_top_llayout_search" />
    </RelativeLayout>
    <!-- 顶部蓝色工具栏结束 -->

    <!-- title -->
    <RelativeLayout
        android:id="@+id/fenlei_title_rl"
        android:layout_width="match_parent"
        android:layout_height="28dp"
        android:layout_marginTop="20dp"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="15dp" >
        <TextView
            android:id="@+id/fenlei_title_tv"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="@string/fenlei_top_llayout_fenlei"
            android:gravity="center"
            android:background="@drawable/fenlei_btn_fillet"
            android:textSize="15sp"
            android:textColor="@color/fenlei_btnlist_btn_textcolor"/>
    </RelativeLayout>
    <!-- title_over -->

    <!-- btnlist -->
    <LinearLayout 
        android:id="@+id/fujin_btnlist_tl"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_marginTop="15dp"
        android:orientation="vertical">
    </LinearLayout>
    <!-- btnlist_over -->

按钮列表是用代码自动生成的GridView,item是TextView。

//Activity代码如下:

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_quanbufenlei);

        initClassify();
        addview();
    }

    private void initClassify(){
        //此处是初始化一些数据,就不贴了
    }


    private void addview(){
        for(int i=1; i<=oc.getOneClassifylist().size();i++){
            ll=(LinearLayout) findViewById(R.id.fujin_btnlist_tl);
             switch (i) {
            case 1:
                addOneClassify(R.drawable.fujin_btnlist_iv_meishi, 
                        R.string.fujin_btnlist_tv_meishi);
                addTwoClassify(tc.getCatelist());
                break;
            case 2:
                addOneClassify(R.drawable.fujin_btnlist_iv_dianying, 
                        R.string.fujin_btnlist_tv_dianying);
                addTwoClassify(tc.getMovielist());
                break;
            case 3:
                addOneClassify(R.drawable.fujin_btnlist_iv_xiuxian, 
                        R.string.fujin_btnlist_tv_xiuxian);
                addTwoClassify(tc.getFallowlist());
                break;
            case 4:
                addOneClassify(R.drawable.fujin_btnlist_iv_wanggou, 
                        R.string.fujin_btnlist_tv_wanggou);
                addTwoClassify(tc.getShoplist());
                break;
            case 5:
                addOneClassify(R.drawable.fujin_btnlist_iv_shenghuo, 
                        R.string.fujin_btnlist_tv_shenghuo);
                addTwoClassify(tc.getLifelist());
                break;
            case 6:
                addOneClassify(R.drawable.fujin_btnlist_iv_jiudian, 
                        R.string.fujin_btnlist_tv_jiudian);
                addTwoClassify(tc.getGropshoplist());
                break;
            case 7:
                addOneClassify(R.drawable.fujin_btnlist_iv_lvyou, 
                        R.string.fujin_btnlist_tv_lvyou);
                addTwoClassify(tc.getTravellist());
                break;
            case 8:
                addOneClassify(R.drawable.fujin_btnlist_iv_liren, 
                        R.string.fujin_btnlist_tv_lireni);
                addTwoClassify(tc.getBeautylist());
                break;
            default:
                break;
            }
        }
    }

    /**
     * 获取Drawable类型的Img
     * @param id
     * @return
     */
    private BitmapDrawable getDrawableFromId(int id){  
        Resources res = getResources();  
        return (BitmapDrawable)res.getDrawable(id);  
    } 

    /**
     * 创建一级分类行
     * @param draimg
     * @param text
     */
    private void addOneClassify(int draimg ,int text){
        LinearLayout ll_one=new LinearLayout(FenleiActivity.this);
        ll_one.setPadding(15, 15, 0, 0);
        //oneclassifyimg
        oneClassify_img=new ImageView(FenleiActivity.this);
        oneClassify_img.setImageDrawable(getDrawableFromId(draimg));
        //onclassifytext
        oneClassify_text=new TextView(FenleiActivity.this);
        oneClassify_text.setText(text);
        oneClassify_text.setTextSize(15);
        oneClassify_text.setPadding(5, 3, 0, 0);

        ll_one.addView(oneClassify_img);
        ll_one.addView(oneClassify_text);

        ll.addView(ll_one, new LinearLayout.LayoutParams( 
                LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)); 
    }

    /**
     * 创建二级buttonlist(GridView)
     * @param btntext
     */
    private void addTwoClassify(List<String> Two_classify_list){

        gridView=new GridView(FenleiActivity.this);
        gridView.setVerticalSpacing(10);
        gridView.setHorizontalSpacing(15);
        gridView.setColumnWidth(60);
        gridView.setNumColumns(3);

        hashlist=new ArrayList<HashMap<String,Object>>();
        for(int j=0 ;j<Two_classify_list.size();j++){
            HashMap<String, Object> map= new HashMap<String, Object>();
            map.put("fenlei_btnlist_btn_text", Two_classify_list.get(j));
            hashlist.add(map);
        }
        sAdapter=new SimpleAdapter(this,hashlist,
                R.layout.fenlei_btnlist_item,
                new String[]{"fenlei_btnlist_btn_text"},new int[]{R.id.fenlei_btnlist_btn_text});
        gridView.setAdapter(sAdapter);
        LinearLayout ll_two=new LinearLayout(FenleiActivity.this);
        ll_two.setPadding(10, 0, 0, 0);

        ll_two.addView(gridView);
        ll.addView(ll_two, new LinearLayout.LayoutParams( 
                LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)); 

    }
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

5条回答 默认 最新

  • danielinbiti
    danielinbiti 2015-05-23 08:47
    已采纳

    btnlist那一部分改改,增加scrollview

     <LinearLayout 
     android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_marginTop="15dp"
            android:orientation="vertical">
        <ScrollView 
            android:layout_width="fill_parent" 
            android:layout_height="fill_parent"
            android:scrollbars="vertical">
            <LinearLayout 
                android:id="@+id/fujin_btnlist_tl"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_marginTop="15dp"
                android:orientation="vertical">
            </LinearLayout>
        </scrollView
    </LinearLayout>
    
    点赞 评论
  • u011732740
    SaluteWarmTinct 2015-05-23 12:10

    图片说明

    点赞 评论
  • yuan_love
    java-缘 2015-05-24 02:32

    加滚动没,高度不够啊

    点赞 评论
  • crazy1235
    月盡天明 2015-05-24 02:48

    使用scrollvew嵌套yi下

    点赞 评论
  • u011732740
    SaluteWarmTinct 2015-05-24 06:23

    现在的布局:

     <?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" >
    
            <ImageView
                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:src="@drawable/fujin_top_llayout_lessthan" />
    
            <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="@string/fenlei_top_llayout_fenlei"
                android:textColor="#fff"
                android:textSize="16sp" />
    
            <ImageView
                android:id="@+id/fujin_top_llayout_dingwei"
                android:layout_width="70dp"
                android:layout_height="30dp"
                android:layout_toLeftOf="@+id/fujin_top_llayout_search"
                android:src="@drawable/iocn2" />
    
            <ImageView
                android:id="@+id/fujin_top_llayout_search"
                android:layout_width="wrap_content"
                android:layout_height="30dp"
                android:layout_alignParentRight="true"
                android:paddingRight="12dp"
                android:src="@drawable/fujin_top_llayout_search" />
        </RelativeLayout>
        <!-- 顶部蓝色工具栏结束 -->
    
        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fillViewport="true"
            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:background="@drawable/fenlei_btn_fillet"
                        android:gravity="center"
                        android:text="@string/fenlei_top_llayout_fenlei"
                        android:textColor="@color/fenlei_btnlist_btn_textcolor"
                        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>
    

    现在的Activity:

     package com.zb.stlala.activity;
    
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    
    import com.zb.stlala.R;
    import com.zb.stlala.entity.OneClassify;
    import com.zb.stlala.entity.TwoClassify;
    
    import android.app.Activity;
    import android.content.res.Resources;
    import android.graphics.drawable.BitmapDrawable;
    import android.os.Bundle;
    import android.view.ViewGroup.LayoutParams;
    import android.widget.Button;
    import android.widget.GridView;
    import android.widget.ImageView;
    import android.widget.LinearLayout;
    import android.widget.SimpleAdapter;
    import android.widget.TableLayout;
    import android.widget.TableRow;
    import android.widget.TextView;
    
    public class FenleiActivity extends Activity {
    
        TwoClassify tc=new TwoClassify();
        OneClassify oc=new OneClassify();
        //TableLayout tl;
        LinearLayout ll;
    
        ImageView oneClassify_img;
        TextView oneClassify_text;
        Button twoClassify_btn;
        GridView gridView;
        ArrayList<HashMap<String, Object>> hashlist;
        SimpleAdapter sAdapter;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_quanbufenlei);
    
            initClassify();
            addview();
        }
    
        private void initClassify(){
            //初始化
        }
    
    
        private void addview(){
            for(int i=1; i<=oc.getOneClassifylist().size();i++){
                ll=(LinearLayout) findViewById(R.id.fujin_btnlist_tl);
                 switch (i) {
                case 1:
                    addOneClassify(R.drawable.fujin_btnlist_iv_meishi, 
                            R.string.fujin_btnlist_tv_meishi);
                    addTwoClassify(tc.getCatelist());
                    break;
                case 2:
                    addOneClassify(R.drawable.fujin_btnlist_iv_dianying, 
                            R.string.fujin_btnlist_tv_dianying);
                    addTwoClassify(tc.getMovielist());
                    break;
                case 3:
                    addOneClassify(R.drawable.fujin_btnlist_iv_xiuxian, 
                            R.string.fujin_btnlist_tv_xiuxian);
                    addTwoClassify(tc.getFallowlist());
                    break;
                case 4:
                    addOneClassify(R.drawable.fujin_btnlist_iv_wanggou, 
                            R.string.fujin_btnlist_tv_wanggou);
                    addTwoClassify(tc.getShoplist());
                    break;
                case 5:
                    addOneClassify(R.drawable.fujin_btnlist_iv_shenghuo, 
                            R.string.fujin_btnlist_tv_shenghuo);
                    addTwoClassify(tc.getLifelist());
                    break;
                case 6:
                    addOneClassify(R.drawable.fujin_btnlist_iv_jiudian, 
                            R.string.fujin_btnlist_tv_jiudian);
                    addTwoClassify(tc.getGropshoplist());
                    break;
                case 7:
                    addOneClassify(R.drawable.fujin_btnlist_iv_lvyou, 
                            R.string.fujin_btnlist_tv_lvyou);
                    addTwoClassify(tc.getTravellist());
                    break;
                case 8:
                    addOneClassify(R.drawable.fujin_btnlist_iv_liren, 
                            R.string.fujin_btnlist_tv_lireni);
                    addTwoClassify(tc.getBeautylist());
                    break;
                default:
                    break;
                }
            }
        }
    
        /**
         * 获取Drawable类型的Img
         * @param id
         * @return
         */
        private BitmapDrawable getDrawableFromId(int id){  
            Resources res = getResources();  
            return (BitmapDrawable)res.getDrawable(id);  
        } 
    
        /**
         * 创建一级分类行
         * @param draimg
         * @param text
         */
        private void addOneClassify(int draimg ,int text){
            LinearLayout ll_one=new LinearLayout(FenleiActivity.this);
            ll_one.setPadding(15, 15, 0, 0);
            //oneclassifyimg
            oneClassify_img=new ImageView(FenleiActivity.this);
            oneClassify_img.setImageDrawable(getDrawableFromId(draimg));
            //onclassifytext
            oneClassify_text=new TextView(FenleiActivity.this);
            oneClassify_text.setText(text);
            oneClassify_text.setTextSize(15);
            oneClassify_text.setPadding(5, 3, 0, 0);
    
            ll_one.addView(oneClassify_img);
            ll_one.addView(oneClassify_text);
    
            ll.addView(ll_one, new LinearLayout.LayoutParams( 
                    LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)); 
        }
    
        /**
         * 创建二级buttonlist(GridView)
         * @param btntext
         */
        private void addTwoClassify(List<String> Two_classify_list){
    
            gridView=new GridView(FenleiActivity.this);
            gridView.setVerticalSpacing(10);
            gridView.setHorizontalSpacing(15);
            gridView.setNumColumns(3);
    
            hashlist=new ArrayList<HashMap<String,Object>>();
            for(int j=0 ;j<Two_classify_list.size();j++){
                HashMap<String, Object> map= new HashMap<String, Object>();
                map.put("fenlei_btnlist_btn_text", Two_classify_list.get(j));
                hashlist.add(map);
            }
            sAdapter=new SimpleAdapter(this,hashlist,
                    R.layout.fenlei_btnlist_item,
                    new String[]{"fenlei_btnlist_btn_text"},new int[]{R.id.fenlei_btnlist_btn_text});
            gridView.setAdapter(sAdapter);
            LinearLayout ll_two=new LinearLayout(FenleiActivity.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)); 
    
        }
    }
    
    

    如果设置固定高 在创建二级btnlist里,动态生成GridView的外层LL那里,效果图是这样的:
    图片说明

    点赞 评论

相关推荐