gpt4 book ai didi

android - 以编程方式设置 LayoutParams.BELOW

转载 作者:太空狗 更新时间:2023-10-29 16:36:36 25 4
gpt4 key购买 nike

我正在尝试以编程方式设置 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;

但它没有正确定位。知道为什么它定位不佳吗?

最佳答案

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

ImageView imageView = new ImageView(this.getActivity());
imageView.setId(View.generateViewId());
...
params.addRule(RelativeLayout.BELOW, imageView.getId());

关于android - 以编程方式设置 LayoutParams.BELOW,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27297974/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com