如何在android中以编程方式将内部线性布局放入线性布局中

时间:2014-12-29 08:25:48

标签: android android-layout

我使用了以下代码,但显示了布局的左上角。但我想要在右下角。

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

}

更多代码:http://pastebin.com/S5uiL1mG 我没弄明白为什么,但我把重力放在右下方但显示在左上方。

1 个答案:

答案 0 :(得分:0)

因为LinearLayout不是固定大小,所以如果不使用肮脏的技巧,这是不可能的。当然可以做你要求的事情(即使很复杂),但我宁愿告诉你的问题揭示了一个糟糕的设计。如果您确实要将对齐的内容对齐,请使用RelativeLayout