Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了android – 以编程方式无法在RelativeLayout中对齐ImageView大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

我无法将imageview添加到相对布局中.我想将图像添加到我使用RelativeLayout动态创建的菜单项列表中.我的所有菜单项都显示正常并且按顺序排列但是当我尝试为每个项目添加图像时,我只得到一个箭头并且它不是垂直居中的.以下是我的代码.

在我的XML文件

在我的代码中:

private void buildMenu(String name,int id) {

    String[] menuItems = getresources().getStringArray(pMenus[id]);
    // Get the rel layout from xml
    RelativeLayout container = (RelativeLayout) findViewById(R.id.pMenu);

    int imagEID=1;
    Typeface tf = Typeface.createFromAsset(this.getAssets(),"mreavesmodot-reg.otf");
    for(String menuItem: menuItems) {           

        // Defining the layout parameters
        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);


        StyledButton menuImage = new StyledButton(this);
        menuImage.setBACkgroundresource(R.drawable.menu_button);
        menuImage.setText(menuItem);
        menuImage.setTypeface(tf);
        menuImage.setTextSize(19);
        menuImage.setPadding(20,0);
        menuImage.setTextColor(Color.WHITE);
        menuImage.setGravity(Gravity.LEFT | Gravity.CENTER_VERTICAL);
        menuImage.setOnClickListener(getOnClickListener(menuImage,Name));
        menuImage.setId(imagEID);

        if(imagEID==1) {
            lp.addRule(RelativeLayout.ALIGN_PARENT_TOp);
        } else {
            lp.addRule(RelativeLayout.bELOW,imagEID-1);
        }
        menuImage.setLayoutParams(lp);


        ImageView arrow = new ImageView(this);
        arrow.setImageresource(R.drawable.arrow_menu);
        arrow.setPadding(0,15,0);
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT );
        params.addRule(RelativeLayout.ALIGN_RIGHT,menuImage.getId());
        params.addRule(RelativeLayout.CENTER_VERTICAL);

        arrow.setLayoutParams(params);

        container.addView(menuImagE);
        container.addView(arrow);

        imagEID++;
    }
}
最佳答案
我认为下面这行是你的问题

params.addRule(RelativeLayout.CENTER_VERTICAL); 

是的,您最有可能添加多个箭头,它们只是一个在彼此之上,所有这些都与完整相对布局的垂直中心对齐.该命令不会对您的按钮项执行垂直居中,而是对父亲RelativeLayout执行.

大佬总结

以上是大佬教程为你收集整理的android – 以编程方式无法在RelativeLayout中对齐ImageView全部内容,希望文章能够帮你解决android – 以编程方式无法在RelativeLayout中对齐ImageView所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。