java代码动态生成控件

来源:互联网 发布:excel表格导入数据库 编辑:程序博客网 时间:2024/04/30 06:24

java代码动态生成控件,代码比较简单,界面也相对比较单调,不过如何通过代码动态生成控件等,写的很明确。

public class MainActivity extends Activity implements OnClickListener{private RelativeLayout mParentLayout;  //父布局private FragmentManager mFragmentManager;  //管理fragment//四个fragment布局private ConversationFragment conversation;private ContactFragment contact;private PluginFragment plugin;private SetupFragment setup;//底部tab栏标签 未选中时状态private int[] normalTabIcons = {R.drawable.tab_icon_conversation_normal,R.drawable.tab_icon_contact_normal,R.drawable.tab_icon_plugin_normal,R.drawable.tab_icon_setup_normal};//底部tab栏标签 选中时状态private int[] downTabIcons = {R.drawable.tab_icon_conversation_selected,R.drawable.tab_icon_contact_selected,R.drawable.tab_icon_plugin_selected,R.drawable.tab_icon_setup_selected};//tab 四个按钮idprivate int[] ids = {R.id.conversationbtn,R.id.contactbtn,R.id.plugin,R.id.setup};//屏幕宽private int screenWidth = 0;private List<ImageView> tabBtns = new ArrayList<ImageView>();  //底部栏按钮@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);initView();  //初始化界面addTab();   //动态增加底部栏setTabSelection(0);  //进来时默认选中}private ImageView originalBtn = null;private void setTabSelection(int index) {originalBtn = tabBtns.get(index);//清空tab栏状态clearSelectionState();FragmentTransaction transaction = mFragmentManager.beginTransaction();  //开启事务//隐藏所有 fragment hideAll(transaction);switch (index) {// 会话case 0:if (conversation == null){conversation = new ConversationFragment();transaction.add(R.id.parentLayout, conversation);} else {transaction.show(conversation);}break;// 联系人case 1:if (contact == null){contact = new ContactFragment();transaction.add(R.id.parentLayout, contact);} else {transaction.show(contact);}break;//动态case 2:if (plugin == null){plugin = new PluginFragment();transaction.add(R.id.parentLayout, plugin);} else {transaction.show(plugin);}break;//设置case 3:if (setup == null){setup = new SetupFragment();transaction.add(R.id.parentLayout, setup);} else {transaction.show(setup);}break;default:break;}originalBtn.setBackgroundResource(downTabIcons[index]);transaction.commit();  //提交事务}private void clearSelectionState(){for (int i = 0; i < ids.length; i++) {tabBtns.get(i).setBackgroundResource(normalTabIcons[i]);}}private void hideAll(FragmentTransaction transaction){if (contact != null) {transaction.hide(contact);}if (conversation != null) {transaction.hide(conversation);}if (plugin != null) {transaction.hide(plugin);}if (setup != null) {transaction.hide(setup);}}//增加底部栏private void addTab(){//底部栏icon 大小 120x118screenWidth = Contents.WIDTH;int btnHeight = (int) ((((float)screenWidth) / 4) *((float)118/120));  //按钮高度//底部栏布局生成/* * 这里的LayoutParams 需要用对应布局下得LayoutParams * 如 父布局是RelativeLayout 就用android.widget.RelativeLayout.LayoutParams *  *   父布局是LinearLayout 就用android.widget.LinearLayout.LayoutParams */LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); LinearLayout tabLayout = new LinearLayout(getApplication());params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);tabLayout.setId(R.id.tablayout);tabLayout.setOrientation(LinearLayout.HORIZONTAL);//将新建的view 加入到父布局中mParentLayout.addView(tabLayout, params);//底部栏按钮生成android.widget.LinearLayout.LayoutParams pa = null;ImageView tabBtn = null;tabBtns.clear();for (int i = 0; i < ids.length; i++) {pa = new android.widget.LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);tabBtn = new ImageView(getApplication());pa.width = screenWidth / 4;pa.height = btnHeight;tabBtn.setId(ids[i]);tabBtn.setBackgroundResource(normalTabIcons[i]);tabBtn.setOnClickListener(this);//将新建的view 加入到父布局中tabLayout.addView(tabBtn, pa);tabBtns.add(tabBtn);}}private void initView(){mParentLayout = (RelativeLayout) findViewById(R.id.parentLayout);mFragmentManager = getFragmentManager();}@Overridepublic void onClick(View v) {switch (v.getId()) {//会话case R.id.conversationbtn:setTabSelection(0);break;//消息case R.id.contactbtn:setTabSelection(1);break;//动态case R.id.plugin:setTabSelection(2);break;//设置case R.id.setup:setTabSelection(3);break;}}}


源码下载地址:http://download.csdn.net/detail/endlife99/7334993

0 0
原创粉丝点击