taocgz 2016-09-08 03:20 采纳率: 0%
浏览 738
已结题

自定义复合控件,这段代码有什么错误,在布局里显示不了。

public class Payment extends RelativeLayout {

private ImageView mImageView;
private TextView mTextView;
private RadioButton mRadioButton;

public Payment(Context context){
    this(context, null);
}

public Payment(Context context, AttributeSet attrs){
    this(context, null, 0);
}

public Payment(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    // TODO Auto-generated constructor stub
    TypedArray mTypedArray = context.obtainStyledAttributes(attrs, R.styleable.payment);//获取自定义属性和默认值             
    int paymentLogoRes = mTypedArray.getInt(R.styleable.payment_paymentlogores, R.drawable.other);
    int paymentLogoId = mTypedArray.getInt(R.styleable.payment_paymentlogoid, 0);   
    String paymentName = mTypedArray.getString(R.styleable.payment_paymentname);
    int paymentNameId = mTypedArray.getInt(R.styleable.payment_paymentnameid, 0);
    int paymentradiobuttonid = mTypedArray.getInt(R.styleable.payment_paymentradiobuttonid, 0);
    mTypedArray.recycle();

    RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    mImageView = new ImageView(context);

    lp.addRule(RelativeLayout.CENTER_VERTICAL);
    mImageView.setLayoutParams(lp);
    mImageView.setId(paymentLogoId);
    mImageView.setImageResource(paymentLogoRes);

    mTextView = new TextView(context);
    lp.addRule(RelativeLayout.ALIGN_RIGHT, mImageView.getId());
    lp.setMargins(10, 0, 0, 0);
    mTextView.setLayoutParams(lp);
    mTextView.setId(paymentNameId);
    mTextView.setText(paymentName);

    mRadioButton = new RadioButton(context);
    lp.addRule(RelativeLayout.ALIGN_RIGHT, 0);
    lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); 
    lp.setMargins(0, 0, 0, 0);
    mRadioButton.setLayoutParams(lp);
    mRadioButton.setId(paymentradiobuttonid);
    mRadioButton.setClickable(false);      

    setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            mRadioButton.setChecked(true);
        }
    });

}

public void setPaymentName(String name) {
    mTextView.setText(name);
}

public void setPaymentLogo(int resId) {
    mImageView.setImageResource(resId);
}

public View getRadioButton() {
    return mRadioButton;
}

}


  • 写回答

2条回答 默认 最新

  • C盘无限大 2016-09-08 06:45
    关注

    你都没有把new好的控件addView到父控件中,肯定没显示。

    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器