gpt4 book ai didi

android - 初始化 Imageview 的 LayoutParams 为空

转载 作者:行者123 更新时间:2023-11-30 03:17:26 24 4
gpt4 key购买 nike

我正在尝试从已添加到 RelativeLayoutImageView 获取 LayoutParams。问题是当我调用我的方法来获取 LayoutParams 时,它没有我之前应用的任何规则或边距。

在我的代码中,您会看到我添加了左对齐或右对齐以及各种边距以让它们形成一个圆圈。但是当我再次尝试获取 LayoutParams 时,它没有规则或边距。

你可以看到我的每条规则的 System.out.println() 并且它总是显示 0,尽管我已经对其应用了左对齐或右对齐。

感谢帮助,代码可能看起来很困惑。我正在调用的方法 - swap() - 是从匿名内部类调用的,因为我需要它在单击每个 ImageView 时更改其位置。

如何获取另一个 ImageViewLayoutParams 并将相同的对齐方式和边距应用于不同的 ImageView

public class HelloMoonFragment extends Fragment {

ArrayList<ImageView> imgViews = new ArrayList<ImageView>();
View v;

//Method I have created
public int swap(int a){

ArrayList<ImageView> copy = new ArrayList<ImageView>(imgViews);

for(int i = 0; i < imgViews.size(); i++){
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(imgViews.get(i).getLayoutParams());

int[] rules;
rules = params.getRules();
for(int x: rules){
params.addRule(x);
System.out.println("Rule: " + x); //Prints out 0??
}

v.findViewById(copy.get(i).getId()).setLayoutParams(params);
}


imgViews = copy;

return 0;
}

@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setRetainInstance(true);
}


//Initialises each ImageView and adds to ArrayList
public ImageView initializeImgViews(Activity a,final int id,final int imageResource1,final int imageResource2,int layoutFeature1,int marginLeft,int marginRight,int marginTop,int height){

//Create new ImageView
final ImageView im = new ImageView(a);

//Set id & image source
im.setId(id);
im.setImageResource(imageResource1);

//Set out Layout
RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
params1.addRule(layoutFeature1);
params1.setMargins(marginLeft,marginTop,marginRight,10);
im.setLayoutParams(params1);

//Edit Image Height & Width
im.getLayoutParams().height = height;
im.getLayoutParams().width = height;

//Set up Listener
im.setTag("im1");
im.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View view) {

swap(id);

if (im.getTag().equals("im1")){

im.setImageResource(imageResource2);
im.setTag("im2");
}
else{
im.setImageResource(imageResource1);
im.setTag("im1");

}
}
});

return im;
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState){
v = inflater.inflate(R.layout.fragment_hello_moon, parent, false);

RelativeLayout tl = (RelativeLayout) v.findViewById(R.id.l1);
tl.setBackgroundColor(Color.RED);

DisplayMetrics metrics = this.getResources().getDisplayMetrics();
int width = metrics.widthPixels;


int numberOfBells = 8;
int HeightOfParent = 350;

final float s = getActivity().getResources().getDisplayMetrics().density;
int scale = (int) ((-20 + HeightOfParent/ ((int) (numberOfBells + 1)/2) * s + 0.5f));

int pos = ((int) (width/2 - scale*1.2));
int posX = (int) (HeightOfParent - scale * 0.6);

double a = 0;
double b = 0;


for (int i = 0; imgViews.size() < numberOfBells; i++ ){

a = pos - pos * Math.sin(Math.toRadians((i) * 180/ (int) ((numberOfBells - 1 )/ 2)));
b = posX - posX * Math.cos(Math.toRadians((i) * 180/ (int) ((numberOfBells - 1 )/ 2)));

imgViews.add(initializeImgViews(getActivity(),(numberOfBells/2) - i,R.drawable.bell_dl_256,R.drawable.bell_ul_256,RelativeLayout.ALIGN_PARENT_LEFT,
(int) a,0,(int) b,scale - (i)));

imgViews.add(initializeImgViews(getActivity(),(numberOfBells/2) + i + 1,R.drawable.bell_dr_256,R.drawable.bell_ur_256,RelativeLayout.ALIGN_PARENT_RIGHT,
0,(int) a,(int) b,scale + ((i + 1))));

}

for(ImageView i : imgViews){
System.out.println("initialise b "+i.getId());
tl.addView(i);
}

tl.addView(txt);

return v;
}

}

最佳答案

ImageView i1 = (Imageview)findViewById(R.Id.img1);
LayoutParams lp = i1.getlayoutparams();

ImageView i2 = new ImageView(this);
i2.setLayoutParams(lp);

关于android - 初始化 Imageview 的 LayoutParams 为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19787159/

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