简体   繁体   中英

Programmatically Can't Align ImageView in 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.

In my XML file

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

        </RelativeLayout>

In my code:

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); 

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM