编程方式无法在RelativeLayout中对齐ImageVie

编程方式无法在RelativeLayout中对齐ImageVie

本文介绍了以编程方式无法在RelativeLayout中对齐ImageView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在将imageview添加到相对布局时遇到麻烦.我想将图像添加到使用RelativeLayout动态创建的菜单项列表中.我所有的菜单项都显示得很好并且井井有条,但是当我尝试将图像添加到每个项时,我只有一个箭头,并且没有垂直居中.下面是我的代码.

I'm having trouble adding an imageview to a relative layout. I would like to add an image to a list of menu items that I am creating dynamically using RelativeLayout. All of my menu items show up just fine and in order but when I try to add an image to each of the items I only get one arrow and it is not centered vertically. Below is my code.

在我的XML文件中

<RelativeLayout
            android:id="@+id/pMenu"
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

        </RelativeLayout>

在我的代码中:

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, 0, 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, 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++;
    }
}

推荐答案

我认为下面的行是您的问题

I think the line below is your problem

params.addRule(RelativeLayout.CENTER_VERTICAL);

是的,您很可能会添加多个箭头,它们只是彼此完全重叠的一个箭头,全部与完整相对布局的垂直中心对齐.该命令不会针对您的按钮项执行垂直居中,而是再次使父项RelativeLayout居中.

YES, you are most likely adding multiple arrows, they are just simply one on top of each other ALL aligned to the vertical center of the full relative layout. That command is not performing a vertical centering against your button item, but agains the parent RelativeLayout.

这篇关于以编程方式无法在RelativeLayout中对齐ImageView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 09:48