Android程序中动态添加Button
来源:互联网 发布:qq微信抢红包软件 编辑:程序博客网 时间:2024/05/25 19:59
在代码中动态添加Button的难点是设置Btn的margin,其它的都好说,具体代码如下:
1、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:id="@+id/btnListLayout" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@android:color/transparent" ></LinearLayout></LinearLayout>
2、code:
public class AddBtnByCodeActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView( R.layout.add_btn_by_code_layout );findAllView( );generateBtnList( getBtnContentList( ) );}private void findAllView( ){mBtnListLayout = ( LinearLayout )findViewById( R.id.btnListLayout );}private ArrayList<String> getBtnContentList( ){ArrayList<String> btnContentList = new ArrayList<String>( );for( int index = 0; index < 10; index++ ){btnContentList.add( "动态Btn" + index );}return btnContentList;}private void generateBtnList( ArrayList<String> btnContentList ){if( null == btnContentList ){return;}mBtnListLayout.removeAllViews( );int index = 0;for( String btnContent : btnContentList ){Button codeBtn = new Button( this );setBtnAttribute( codeBtn, btnContent, index, Color.TRANSPARENT, Color.BLACK, 24 );mBtnListLayout.addView( codeBtn );index++;}}private void setBtnAttribute( Button codeBtn, String btnContent, int id, int backGroundColor, int textColor, int textSize ){if( null == codeBtn ){return;}codeBtn.setBackgroundColor( ( backGroundColor >= 0 )?backGroundColor:Color.TRANSPARENT );codeBtn.setTextColor( ( textColor >= 0 )?textColor:Color.BLACK );codeBtn.setTextSize( ( textSize > 16 )?textSize:24 );codeBtn.setId( id );codeBtn.setText( btnContent );codeBtn.setGravity( Gravity.LEFT );codeBtn.setOnClickListener( new OnClickListener( ) {@Overridepublic void onClick(View v) {// btn click process}});RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT );rlp.addRule( RelativeLayout.ALIGN_PARENT_LEFT );codeBtn.setLayoutParams( rlp );}private LinearLayout mBtnListLayout = null;}
- Android程序中动态添加Button
- Android程序中动态添加Button
- C# 动态添加Button
- 动态添加button按钮
- Android 在程序中动态添加 View 布局或控件
- 向Android listview中添加button
- android中button控件的添加
- MFC中动态创建BUTTON及添加响应的响应
- Sencha Touch 2中如何动态添加button
- Sencha Touch 2中如何动态添加button
- C#winform中动态添加button并绑定事件
- UGUI中Button和Toggle 添加动态事件
- unity3D 在UGUI中动态添加button的监听事件
- extjs panel动态添加button
- 动态添加UIActionSheet cancel button
- android ListView添加button
- android中动态添加组件
- android 中动态添加view
- Ubuntu下好用的轻量级代码编辑器—Scribes
- CentOS 重启网卡提示:/org/freedesktop/NetworkManager/ActiveConnection/(n)
- Android项目实战--手机卫士02--与服务器交互
- DB2_备份恢复(离线&在线)
- 退出python shell
- Android程序中动态添加Button
- Android---ASE---脚本语言研究1--perl
- 原始字符串
- VS2010在C盘下生成的.iTrace文件解决办法
- 列表和元组
- 字符串处理
- 字典,python中的map
- 3.3 E7
- 代码块