LayoutParams的一般用法

本文介绍了在Android开发中View如何通过LayoutParams类告知其父视图所需的大小,并提供了具体的代码示例,包括如何设置自定义的RadioButton及其布局参数。

View通过LayoutParams类告诉其父视图它想要地大小(即,长度和宽度)。

,每个View都包含一个ViewGroup.LayoutParams类或者其派生类,View类依赖于ViewGroup.LayoutParams。

代码例如:

     public class DomainRegisterLogic {
 Activity mContext;
 private String domainName;
 private String suffixString;
 private CheckBox comCheckbox, cnCheckbox, netCheckbox, comcnCheckbox,
   orgCheckbox, ccCheckbox, netcnCheckbox, orgcnCheckbox,
   chinaCheckbox, checkBoxAsia, checkBoxBiz, checkBoxInfo, checkBoxTv,
   checkBoxTw, checkBoxIn, checkBoxMe, checkBoxPw;
 private View viewP1, viewP2;
 private LayoutInflater layoutInflater;
 private List<View> views;
 private ArrayList<String> suffixList;
 private RadioGroup.LayoutParams layoutParamsRG;
 
 public DomainRegisterLogic(Activity activity) {
  mContext = activity;
  layoutInflater = LayoutInflater.from(mContext);
  views = new ArrayList<View>();
  suffixList = new ArrayList<String>();
  layoutParamsRG = new RadioGroup.LayoutParams(CommonFunction.dip2px(
    mContext, 5.0f), CommonFunction.dip2px(mContext, 5.0f));
  layoutParamsRG.leftMargin = CommonFunction.dip2px(mContext, 11.0f);
 }
 
 public void initViewPager(ViewPager viewPager, RadioGroup radioGroup) {
  // TODO Auto-generated method stub
  radioGroup.addView(getRadioButton(), layoutParamsRG);
  radioGroup.addView(getRadioButton(), layoutParamsRG);
  ((RadioButton) radioGroup.getChildAt(0)).setChecked(true);
  }
   public RadioButton getRadioButton() {
  RadioButton radioButton = new RadioButton(mContext);
  // radioButton.setLayoutParams(layoutParams);
  radioButton.setButtonDrawable(null);
  radioButton.setBackgroundDrawable(mContext.getResources().getDrawable(
    R.drawable.radio_vp_position_selector));
  return radioButton;
 }
}

转载于:https://my.oschina.net/u/1538508/blog/363883

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值