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 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建