简体   繁体   中英

How to put inside linear layout another linear layout programmatically in android

I used following following code but display top left of the layout.but i want to right bottom.

public class Cmmm extends LinearLayout {

LinearLayout customLinearLayout;

@SuppressLint("NewApi")
public CommericalAds(Context context ,int adWidth, int adHeight, Drawable drawable) {
    super(context);
    LayoutInflater.from(context).inflate(R.layout.custom_view, this, true);

    customLinearLayout = (LinearLayout) findViewById(R.id.customView);

    LinearLayout.LayoutParams layoutParamslinear = new LinearLayout.LayoutParams(
            adsWidth, adsHeight);
    customLinearLayout.setLayoutParams(layoutParamslinear);


    lpViewMiddleText = new LayoutParams(LayoutParams.WRAP_CONTENT,
            LayoutParams.WRAP_CONTENT);
    // Heading Background color and bottom background color and middel text

    llMiddleText = new LinearLayout(mContext);
    llSocialMedia = new LinearLayout(mContext);
    llSocialMedia.setOrientation(HORIZONTAL);

    lpSocialMedia = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
    lpSocialMedia.gravity = Gravity.RIGHT|Gravity.BOTTOM;
    imageView.setBackground(drawable);

    LayoutParams imageGravityParams1 = new LinearLayout.LayoutParams(
            LayoutParams.FILL_PARENT, adHeight-50);
    customLinearLayout.addView(imageView, imageGravityParams1);
    addSocialMedai();

    }
public void addSocialMedai() {
    imgFavourite = new ImageView(mContext);
    imgFavourite.setBackgroundResource(R.drawable.nonfavourite_32);

    imgShare = new ImageView(mContext);
    imgShare.setBackgroundResource(R.drawable.share_32);
    llSocialMedia.addView(imgFavourite);
    llSocialMedia.addView(imgShare);
    customLinearLayout.addView(llSocialMedia, lpSocialMedia);
}

}

more code: http://pastebin.com/S5uiL1mG I didnot figure out why , but i put gravity on bottom right but display top left.

Because a LinearLayout isn't fixed size, this is not possible without using dirty tricks. Of course it is possible to do what you ask (even if complicated), but i'd rather tell that your question reveals a bad design. If you really want to align something bottom right, use instead a 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