简体   繁体   中英

Android set Layout align left and center vertical programmatically

I want to have arrowview overlap on homeview in center vertical left. I have tried to do so using couple of views but didn't work. It shows me overlap but on left top corner instead of left center vertical. Please help me.

private RelativeLayout makeContentView1() {
    RelativeLayout arrowView = new RelativeLayout(this);
    arrowView.setBackgroundResource(R.drawable.image);
    //RelativeLayout.LayoutParams relativeParams = (RelativeLayout.LayoutParams)arrowView.getLayoutParams();
    //relativeParams.setMargins(0, 80, 0, 0);
    //arrowView.setLayoutParams(relativeParams);
    arrowView.setGravity(RelativeLayout.CENTER_VERTICAL | RelativeLayout.ALIGN_LEFT);
    return arrowView;

}

public void makeLayout() {
    homeView = new RelativeLayout(this);
    LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);

    homeView.setLayoutParams(lp);

    bodyView = this.makeBodyView();
    arrowView = this.makeContentView1(300,300);

    homeView.addView(bodyView);
    homeView.addView(arrowView);

    return homeView;

}

You can achive your task by using a single RelativeLayout with applying rules on it, Use the below code to achive this,

private RelativeLayout makeContentView1() {
    RelativeLayout arrowView = new RelativeLayout(this);
    arrowView.setBackgroundResource(R.drawable.image);


    return arrowView;
}

public void makeLayout() {
    homeView = new RelativeLayout(this);
    LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.MATCH_PARENT);

    homeView.setLayoutParams(lp);

    bodyView = this.makeBodyView();
    arrowView = this.makeContentView1(300,300);

    homeView.addView(bodyView);
    homeView.addView(arrowView);

    RelativeLayout.LayoutParams relativeParams =       (RelativeLayout.LayoutParams)arrowView.getLayoutParams();
    relativeParams.addRule(RelativeLayout.CENTER_VERTICAL);
    arrowView.setLayoutParams(relativeParams);
return homeView;

}

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