以编程方式设置LayoutParams.BELOW

Son*_*hja 2 android

我正在尝试以LayoutParams.BELOW编程方式设置.这是我的代码:

RelativeLayout layout = new RelativeLayout(this.getActivity());
    layout.setLayoutParams(new RelativeLayout.LayoutParams(
             LayoutParams.FILL_PARENT,
             LayoutParams.FILL_PARENT));
    layout.setBackgroundColor(Color.parseColor("#CCCDCDCD"));

    ImageView imageView = new ImageView(this.getActivity());
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    params.addRule(RelativeLayout.CENTER_IN_PARENT);
    imageView.setLayoutParams(params);
    imageView.setBackgroundResource(R.drawable.create_template);

    AnimationDrawable frameAnimation = (AnimationDrawable) imageView.getBackground();

    if (frameAnimation != null) {
        frameAnimation.start();
    }

    TextView textView = new TextView(this.getActivity());
    params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    params.addRule(RelativeLayout.BELOW, imageView.getId());
    textView.setLayoutParams(params);
    textView.setText("Generating information...");

    layout.addView(imageView);
    layout.addView(textView);

    return layout;
Run Code Online (Sandbox Code Playgroud)

但它没有正确定位.有什么想法没有定位好吗?

Kar*_*uri 5

ImageView不具有ID.你需要给它一个.定义一些常量整数值,或者使用View.generateViewId(),并setId()在布局参数中使用之前调用:

ImageView imageView = new ImageView(this.getActivity());
imageView.setId(View.generateViewId());
...
params.addRule(RelativeLayout.BELOW, imageView.getId());
Run Code Online (Sandbox Code Playgroud)