API RelativeLayout在API17之前添加规则

时间:2017-06-06 13:12:02

标签: android android-layout

我'用minSdk = 14编写Android应用程序。我有一个RelativeLayout的活动,其中放置了一些视图。当我按下此Activity中的按钮时,我必须在此布局中添加5个视图:2个textviews,2个editTexts和一个ImageView。我试图这样做:

    CustomTextView fromLabel = new CustomTextView(this);
    RelativeLayout.LayoutParams paramsForFromLabel = new RelativeLayout.LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); //ширина, длина
    paramsForFromLabel.addRule(RelativeLayout.BELOW, lastCounterId);
    paramsForFromLabel.topMargin = 10; 
    fromLabel.setId(counterIds.get(5*counterMax)); 
    fromLabel.setGravity(Gravity.CENTER); 
    fromLabel.setText(R.string.labelCommunalCounterFrom); 
    int newFromLabelId = fromLabel.getId(); 

    EditText from = new EditText(this);
    from.setInputType(InputType.TYPE_CLASS_NUMBER);
    RelativeLayout.LayoutParams paramsForFrom = new RelativeLayout.LayoutParams(
            (int) getResources().getDimension(R.dimen.number_width), ViewGroup.LayoutParams.WRAP_CONTENT);
    paramsForFrom.addRule(RelativeLayout.BELOW, lastCounterId);
    paramsForFrom.addRule(RelativeLayout.RIGHT_OF, fromLabel.getId());
    from.setId(counterIds.get(5*counterMax+1));
    int newFromFieldId = from.getId();

    paramsForFromLabel.addRule(RelativeLayout.ALIGN_BASELINE, newFromFieldId); 

    CustomTextView toLabel = new CustomTextView(this);
    RelativeLayout.LayoutParams paramsForToLabel = new RelativeLayout.LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); 
    paramsForFromLabel.addRule(RelativeLayout.BELOW, lastCounterId);
    paramsForToLabel.addRule(RelativeLayout.RIGHT_OF, from.getId()); 
    paramsForFromLabel.topMargin = 10; 
    toLabel.setId(counterIds.get(5*counterMax+2)); 
    toLabel.setGravity(Gravity.CENTER); 
    toLabel.setText(R.string.labelCommunalCounterTo); 
    int newToLabelId = toLabel.getId(); 

    EditText to = new EditText(this);
    to.setInputType(InputType.TYPE_CLASS_NUMBER);
    RelativeLayout.LayoutParams paramsForTo = new RelativeLayout.LayoutParams(
            (int) getResources().getDimension(R.dimen.number_width), ViewGroup.LayoutParams.WRAP_CONTENT);
    paramsForTo.addRule(RelativeLayout.BELOW, lastCounterId);
    paramsForTo.addRule(RelativeLayout.RIGHT_OF, toLabel.getId());
    to.setId(counterIds.get(5*counterMax+3));
    int newToFieldId = to.getId();

    paramsForToLabel.addRule(RelativeLayout.ALIGN_BASELINE, newToFieldId); 

    ImageView dropCounter = new ImageView(this);
    dropCounter.setId(counterIds.get(5*counterMax+4));
    dropCounter.setImageResource(R.drawable.cancel_icon);
    RelativeLayout.LayoutParams paramsForDrop = new RelativeLayout.LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    paramsForDrop.addRule(RelativeLayout.BELOW, lastCounterId);
    paramsForDrop.addRule(RelativeLayout.RIGHT_OF, to.getId());
    paramsForDrop.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);

    layout.addView(fromLabel, paramsForFromLabel);
    layout.addView(from, paramsForFrom);
    layout.addView(toLabel, paramsForToLabel);
    layout.addView(to, paramsForTo);
    layout.addView(dropCounter, paramsForDrop);

问题是:对于第二个视图(EditText from),将忽略规则RIGHT_OF。所有其他字段显示良好。 接下来的问题是:如何重写View的规则?我有一个带有XML指定的LAYOUT_BELOW规则的按钮。如何以编程方式更改它?

1 个答案:

答案 0 :(得分:0)

固定!这听起来很愚蠢,但问题是: 我为我的第一个View(第一个TextField)设置了一个id = 0。当我尝试引用此ID时,此规则被忽略。当我设置ID = 1时 - 所有问题都消失了! 对不起这个问题!在阅读文档时我并不小心:我们可以使用POSITIVE整数作为视图ID。