您的位置:首页 > 移动开发 > Android开发

动态生成控件并设置布局参数

2017-01-18 20:16 417 查看
private final Button mBlankTab;
protected LinearLayout.LayoutParams mBlankBtnParam=null;

mBlankTab=new Button(AbsBaseActivity.this);
mBlankTab.setBackgroundDrawable(getResources().getDrawable(R.drawable.fm_blank_tab));
mBlankBtnParam=new LinearLayout.LayoutParams(
new ViewGroup.MarginLayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT));
mBlankTab.setLayoutParams(mBlankBtnParam);
if(getViewDirection()==ViewGroup.LAYOUT_DIRECTION_LTR){
mBlankBtnParam.setMargins((int)getResources().getDimension(R.dimen.tab_margin_left),0,
(int)getResources().getDimension(R.dimen.tabb_marfin_right),0);//左上右下
}else if(getViewDirection()==ViewGroup.LAYOUT_DIRECTION_RTL){
mBlankBtnParam.setMargins((int)getResources().getDimension(R.dimen.tabb_marfin_right),0,
(int)getResources().getDimension(R.dimen.tab_margin_left),0);
}
mBlankTab.setLayoutParams(mBlankBtnParam);

protected void addTab(String text){
LinearLayout.LayoutParams mlp=null;
mlp=new LinearLayout.LayoutParams(new ViewGroup.MarginLayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.MATCH_PARENT));
TextView txt=new TextView(AbsBaseActivity.this);
txt.setTextColor(getResources().getColor(R.color.zuk_gray));
txt.setText(text);
txt.setTextSize(12f);
txt.setGravity(Gravity.CENTER);
Drawable rightIcon=getResources().getDrawable(R.drawable.zuk_tab_right);
Drawable leftIcon=getResources().getDrawable(R.drawable.zuk_tab_left);
rightIcon.setBounds(0,0,rightIcon.getMinimumWidth(),rightIcon.getMinimumHeight());
leftIcon.setBounds(0,0,leftIcon.getMinimumWidth(),leftIcon.getMinimumHeight());
txt.setCompoundDrawables(leftIcon,null,rightIcon,null);
txt.setLayoutParams(mlp);
viewLikeBtn=txt;

viewLikeBtn.setOnClickListener(AbsBaseActivity.this);
viewLikeBtn.setId(mTabNameList.size());
mTabNameList.add(text);

viewLikeBtn.setOutlineProvider(null);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android