Android程序中动态添加Button

   在代码中动态添加Button的难点是设置Btn的margin,其它的都好说,具体代码如下:

1、xml文件:

[html]  view plain copy
  1. <span style="font-size:18px"><?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout  
  3.     xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent"  
  6.     android:orientation="vertical">  
  7.     <LinearLayout   
  8.         android:id="@+id/btnListLayout"  
  9.         android:orientation="vertical"  
  10.         android:layout_width="fill_parent"  
  11.         android:layout_height="fill_parent"  
  12.         android:background="@android:color/transparent"  
  13.         ></LinearLayout>  
  14. </LinearLayout></span>  

2、code:

[java]  view plain copy
  1. <span style="font-size:18px">public class AddBtnByCodeActivity extends Activity {  
  2.     @Override  
  3.     protected void onCreate(Bundle savedInstanceState) {  
  4.         super.onCreate(savedInstanceState);  
  5.         setContentView( R.layout.add_btn_by_code_layout );  
  6.         findAllView( );  
  7.         generateBtnList( getBtnContentList( ) );  
  8.     }  
  9.       
  10.     private void findAllView( ){  
  11.         mBtnListLayout = ( LinearLayout )findViewById( R.id.btnListLayout );  
  12.     }  
  13.       
  14.     private ArrayList<String> getBtnContentList( ){  
  15.         ArrayList<String> btnContentList = new ArrayList<String>( );  
  16.           
  17.         forint index = 0; index < 10; index++ ){  
  18.             btnContentList.add( "动态Btn" + index );  
  19.         }  
  20.           
  21.         return btnContentList;  
  22.     }  
  23.       
  24.     private void generateBtnList( ArrayList<String> btnContentList ){  
  25.         ifnull == btnContentList ){  
  26.             return;  
  27.         }  
  28.           
  29.         mBtnListLayout.removeAllViews( );  
  30.         int index = 0;  
  31.         for( String btnContent : btnContentList ){  
  32.             Button codeBtn = new Button( this );  
  33.               
  34.             setBtnAttribute( codeBtn, btnContent, index, Color.TRANSPARENT, Color.BLACK, 24 );  
  35.             mBtnListLayout.addView( codeBtn );  
  36.             index++;  
  37.         }  
  38.     }  
  39.       
  40.     private void setBtnAttribute( Button codeBtn, String btnContent, int id, int backGroundColor, int textColor, int textSize ){  
  41.         ifnull == codeBtn ){  
  42.             return;  
  43.         }  
  44.           
  45.         codeBtn.setBackgroundColor( ( backGroundColor >= 0 )?backGroundColor:Color.TRANSPARENT );  
  46.         codeBtn.setTextColor( ( textColor >= 0 )?textColor:Color.BLACK );  
  47.         codeBtn.setTextSize( ( textSize > 16 )?textSize:24 );  
  48.         codeBtn.setId( id );  
  49.         codeBtn.setText( btnContent );  
  50.         codeBtn.setGravity( Gravity.LEFT );  
  51.         codeBtn.setOnClickListener( new OnClickListener( ) {  
  52.             @Override  
  53.             public void onClick(View v) {  
  54.                 // btn click process  
  55.             }  
  56.         });  
  57.           
  58.         RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT );  
  59.         rlp.addRule( RelativeLayout.ALIGN_PARENT_LEFT );  
  60.           
  61.         codeBtn.setLayoutParams( rlp );  
  62.     }  
  63.       
  64.     private LinearLayout mBtnListLayout = null;  
  65. }</span>  

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值