简体   繁体   中英

RelativeLayout with ImageView and TextView - Align TextView below ImageView, both center in RelativeLayout Programatically

I have a RelativeLayout with ImageView and TextView. I want to the TextView right below the ImageView(with a small padding) and both the ImageView and TextView, aligned to center in the RelativeLayout.

The RelativeLayout is added Programatically

I have read some questions on SO regarding the alignment, but, I can't get them to work for me. Below is my current code...

Code for RelativeLayout

RelativeLayout relativeLayout = new RelativeLayout(this);

Code for ImageView

        ImageView image = new ImageView(this);  
        RelativeLayout.LayoutParams lpImage = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);

        lpImage.addRule(RelativeLayout.CENTER_IN_PARENT);

        //Setting the parameters on the Image
        image.setLayoutParams(lpImage);

        //adding imageview to relative layout
        relativeLayout.addView(image);

Code for TextView

TextView textview = new TextView(this);
RelativeLayout.LayoutParams lpTextView = new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.WRAP_CONTENT,
                    RelativeLayout.LayoutParams.WRAP_CONTENT);
            lpTextView.addRule(RelativeLayout.BELOW, image.getId());

            //Setting the parameters on the TextView
            textview.setLayoutParams(lpTextView);

            //Adding TextView to relative layout
            relativeLayout.addView(textview);

If I set RelativeLayout.CENTER_IN_PARENT for both image and text, they overlap eachother, which is understandable as RelativeLayout supports overlapping of views.

I thought setting RelativeLayout.BELOW for textview will make it align itself below the image, but it's not the case. I even tried RelativeLayout.ALIGN_BOTTOM for textview, but even that didn't work.

Try this..

RelativeLayout relativeLayout = new RelativeLayout(this);

RelativeLayout.LayoutParams lprela = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.MATCH_PARENT,
            RelativeLayout.LayoutParams.MATCH_PARENT);
relativeLayout.setLayoutParams(lprela);

ImageView image = new ImageView(this);  
    RelativeLayout.LayoutParams lpImage = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.WRAP_CONTENT,
            RelativeLayout.LayoutParams.WRAP_CONTENT);

    lpImage.addRule(RelativeLayout.ALIGN_PARENT_TOP);

    //Setting the parameters on the Image
    image.setLayoutParams(lpImage);

    //adding imageview to relative layout
    relativeLayout.addView(image);

    TextView textview = new TextView(this);
    RelativeLayout.LayoutParams lpTextView = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
        lpTextView.addRule(RelativeLayout.BELOW, image.getId());

        //Setting the parameters on the TextView
        textview.setLayoutParams(lpTextView);

        //Adding TextView to relative layout
        relativeLayout.addView(textview);

Or

LinearLayout linearLayout = new LinearLayout(this);
LinearLayout.LayoutParams lp_ineer_ver = new LinearLayout.LayoutParams(
              LinearLayout.LayoutParams.MATCH_PARENT, 
              LinearLayout.LayoutParams.MATCH_PARENT);
linearLayout.setGravity(Gravity.CENTER);
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.setLayoutParams(lp_ineer_ver);

ImageView image = new ImageView(this);  
        LinearLayout.LayoutParams lpImage = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);

        //Setting the parameters on the Image
        image.setLayoutParams(lpImage);

        //adding imageview to relative layout
        linearLayout.addView(image);

TextView textview = new TextView(this);
LinearLayout.LayoutParams lpTextView = new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.WRAP_CONTENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT);

            //Setting the parameters on the TextView
            textview.setLayoutParams(lpTextView);

            //Adding TextView to relative layout
            linearLayout.addView(textview);

Below process may also work for you. 1) Add Image view & Text view in vertical LinearLayout. 2) Add the linear layout to Center of Relative layout.(using CENTER_IN_PARENT).

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