java代码动态网布局里面添加控件

private void setListPath(Context context, RelativeLayout footerRelativeLayout, String ButtonText) {  
        if (flagAddFooterGuide) {  
            // 我的音乐按钮  
            ImageView myMusic = new ImageView(context);  
            myMusic.setImageResource(R.drawable.style1_mymusic);  
            myMusic.setOnClickListener(new OnClickListener() {  
                @Override  
                public void onClick(View v) {  
                    // TODO Auto-generated method stub  
                    ((MyMusicActivity) myContext).setSdCardFlag(false);  
                    ((MyMusicActivity) myContext).onCreate(null);  
                }  
   
            });  
            myMusic.setId(1); // 设置ID为1  
   
            ImageView myStorage = new ImageView(context);  
            myStorage.setImageResource(R.drawable.style1_mymusic_usbstore);  
            myStorage.setOnClickListener(new OnClickListener() {  
   
                @Override  
                public void onClick(View v) {  
                    // TODO Auto-generated method stub  
                    // 设置ListView为显示  
                    ((MyMusicActivity) myContext).setlistViewLinearLayoutVisible(false);  
                    // GalleryVisible为隐藏  
                    ((MyMusicActivity) myContext).setGalleryVisible(true);  
                }  
   
            });  
   
            myStorage.setId(2); // 设置id为2  
            // 设置本地当前目录的按钮  
            MyImageButton myImageButtonFolderNow = new MyImageButton(context, R.drawable.style1_mymusic_details, ButtonText);  
   
            RelativeLayout.LayoutParams lp1 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,  
                    ViewGroup.LayoutParams.WRAP_CONTENT);  
            lp1.addRule(RelativeLayout.ALIGN_PARENT_LEFT);  
   
            RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,  
                    ViewGroup.LayoutParams.WRAP_CONTENT);  
            lp2.addRule(RelativeLayout.RIGHT_OF, 1);//在控件ID为1的控件的右边  
   
            RelativeLayout.LayoutParams lp3 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,  
                    ViewGroup.LayoutParams.WRAP_CONTENT);  
            lp3.addRule(RelativeLayout.RIGHT_OF, 2);  
   
            footerRelativeLayout.addView(myMusic, lp1);  
            footerRelativeLayout.addView(myStorage, lp2);  
   
            // 播放按钮  
            MyImageButton myImageButtonPlay = new MyImageButton(context, R.drawable.mymusic_button_pause_backgroud, "播放");  
   
            footerRelativeLayout.addView(myImageButtonFolderNow, lp3);  
   
            myImageButtonPlay.setId(4);  
            RelativeLayout.LayoutParams lp4 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,  
                    ViewGroup.LayoutParams.WRAP_CONTENT);  
   
            lp4.addRule(RelativeLayout.LEFT_OF, 5);  
   
            // 随机按钮  
            MyImageButton myImageButtonPlayRandom = new MyImageButton(context, R.drawable.mymusic_button_play_random, "随机开");  
   
            myImageButtonPlayRandom.setId(5);  
            RelativeLayout.LayoutParams lp5 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,  
                    ViewGroup.LayoutParams.WRAP_CONTENT);  
            lp5.addRule(RelativeLayout.LEFT_OF, 6);  
   
            // 歌词按钮  
            MyImageButton myImageButtonLyric = new MyImageButton(context, R.drawable.mymusic_button_lyric, "LRC歌词");  
            myImageButtonLyric.setId(6);  
            RelativeLayout.LayoutParams lp6 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,  
                    ViewGroup.LayoutParams.WRAP_CONTENT);  
            lp6.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);  
            // 加入  
            footerRelativeLayout.addView(myImageButtonLyric, lp6);  
            footerRelativeLayout.addView(myImageButtonPlayRandom, lp5);  
            footerRelativeLayout.addView(myImageButtonPlay, lp4);  
            flagAddFooterGuide = false;  


案例:

LinearLayout layout = new LinearLayout(this);  
 layout.setOrientation(LinearLayout.VERTICAL);  
 
 btn = new Button(this);  
 btn.setId(101);  
 btn.setText("test looper");  
 btn.setOnClickListener(this);  
 LinearLayout.LayoutParams param =  
     new LinearLayout.LayoutParams(100,50);  
 param.topMargin = 10;  
 layout.addView(btn, param);   
 
 btn2 = new Button(this);  
 btn2.setId(102);  
 btn2.setText("exit");  
 btn2.setOnClickListener(this);  
 layout.addView(btn2, param);  
 
 tv = new TextView(this);  
 tv.setTextColor(Color.WHITE);  
 tv.setText("");  
 //其中 FP 是final int  FP=LinearLayout.LayoutParams.FILL_PARENT;  
 //final int WC = LinearLayout.LayoutParams.WRAP_CONTENT;  
 LinearLayout.LayoutParams param2 =  
    new LinearLayout.LayoutParams(FP, WC);  
 param2.topMargin = 10;  
 param2.setMargins(0, 0, 5, 0);  //参数分别是(int left, int top, int right, int bottom)  
 layout.addView(tv, param2);  
 setContentView(layout);  


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值