Android 如何在framelayout下方动态添加线性布局?

Android 如何在framelayout下方动态添加线性布局?,android,android-framelayout,Android,Android Framelayout,我有一个扩展FrameLayout的类,叫做TinderStackLayout 此类有一个名为“addCard()”的方法,该方法将名为TinderCardView的动态自定义视图膨胀到主stacklayout中 我需要在充气的卡片下面添加一个线性布局 这是我的TinderStackLayout- public class TinderStackLayout extends FrameLayout { // Constants private static final int DURA

我有一个扩展FrameLayout的类,叫做TinderStackLayout

此类有一个名为“addCard()”的方法,该方法将名为TinderCardView的动态自定义视图膨胀到主stacklayout中

我需要在充气的卡片下面添加一个线性布局

这是我的TinderStackLayout-

public class TinderStackLayout extends FrameLayout {

  // Constants
  private static final int DURATION = 300;

  // Variable members
  private OnCardSwipedListener onCardSwipedListener;
  private int screenWidth;
  private int yMultiplier;

  //Top card
  private TinderCardView topCardOnStack;

  private LinearLayout mButtonsContainer;
  private Button mDeleteButton, mPassButton, mApproveButton;


  //Constructors
  public TinderStackLayout(Context context) {
    super(context);
    init();
  }

  public TinderStackLayout(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
  }

  public TinderStackLayout(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    init();
  }

  @Override
  public void addView(View child, int index, ViewGroup.LayoutParams params) {
    super.addView(child, index, params);
    if (onCardSwipedListener != null)
      onCardSwipedListener.onNext(getChildCount());
  }


  @Override
  public void removeView(View view) {
    super.removeView(view);
    if (onCardSwipedListener != null)
      onCardSwipedListener.onNext(getChildCount());
  }

  @Override
  public void onDetachedFromWindow() {
    super.onDetachedFromWindow();
  }

  // Helper Methods
  private void init() {
    setClipChildren(false);
    screenWidth = DisplayUtility.getScreenWidth(getContext());
    yMultiplier = DisplayUtility.dp2px(getContext(), 8);

    mButtonsContainer = new LinearLayout(getContext());
    mButtonsContainer.setOrientation(LinearLayout.HORIZONTAL);

    mDeleteButton = new Button(getContext());
    mDeleteButton.setText("Delete");
    mPassButton = new Button(getContext());
    mPassButton.setText("Pass");
    mApproveButton = new Button(getContext());
    mApproveButton.setText("Approve");

    mButtonsContainer.addView(mDeleteButton);
    mButtonsContainer.addView(mPassButton);
    mButtonsContainer.addView(mApproveButton);

    RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    layoutParams.addRule(RelativeLayout.ALIGN_BOTTOM);
    addView(mButtonsContainer, layoutParams);
  }

  public void addCard(TinderCardView tinderCardView) {
    View firstCard = getChildAt(0);

    if (firstCard != null && firstCard.equals(tinderCardView)) {
      return;
    }

    if (onCardSwipedListener == null)
      onCardSwipedListener = tinderCardView.getOnCardSwipedListener();

    topCardOnStack = tinderCardView;

    ViewGroup.LayoutParams layoutParams;
    layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);

    int childCount = getChildCount();
    addView(tinderCardView, 0, layoutParams);

    float scaleValue = 1 - (childCount / 50.0f);

    tinderCardView.animate()
        .x(0)
        .y(childCount * yMultiplier)
        .scaleX(scaleValue)
        .setInterpolator(new AnticipateOvershootInterpolator())
        .setDuration(DURATION);
  }

  public TinderCardView getTopCardOnStack() {
    return topCardOnStack;
  }
}

正如您所见,我添加了一个带有3个按钮的线性布局,但实际情况是,它在屏幕左上角膨胀,而不是在创建的卡片底部膨胀

如何更改创建的线性布局的位置

编辑-

尝试了Shayan D的建议,这是我新的基于线性布局的课程-

package com.etiennelawlor.tinderstack.ui;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AnticipateOvershootInterpolator;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import com.etiennelawlor.tinderstack.R;
import com.etiennelawlor.tinderstack.bus.events.OnCardSwipedListener;
import com.etiennelawlor.tinderstack.utilities.DisplayUtility;

public class TinderStackLinear extends LinearLayout {

  // Constants
  private static final int DURATION = 300;

  // Variable members
  private OnCardSwipedListener onCardSwipedListener;
  private int screenWidth;
  private int yMultiplier;

  //Top card
  private TinderCardView topCardOnStack;

  private FrameLayout mCardsContainer;
  private LinearLayout mButtonsContainer;
  private Button mDeleteButton, mPassButton, mApproveButton;


  public TinderStackLinear(Context context) {
    super(context);
    init();
  }

  public TinderStackLinear(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
  }

  public TinderStackLinear(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    init();
  }

  @Override
  public void setOrientation(int orientation) {
    super.setOrientation(HORIZONTAL);
  }

  private void init(){
    setClipChildren(false);
    screenWidth = DisplayUtility.getScreenWidth(getContext());
    yMultiplier = DisplayUtility.dp2px(getContext(), 8);

    mCardsContainer = new FrameLayout(getContext()) {
      @Override
      public void addView(View child, int index, ViewGroup.LayoutParams params) {
        super.addView(child, index, params);
        if (onCardSwipedListener != null)
          onCardSwipedListener.onNext(getChildCount());
      }

      @Override
      public void removeView(View view) {
        super.removeView(view);
        if (onCardSwipedListener != null)
          onCardSwipedListener.onNext(getChildCount());
      }

      @Override
      public void onDetachedFromWindow() {
        super.onDetachedFromWindow();
      }

    };

    mButtonsContainer = new LinearLayout(getContext());;
    mButtonsContainer.setOrientation(LinearLayout.HORIZONTAL);

    mDeleteButton = new Button(getContext());
    mDeleteButton.setText(R.string.tinder_stack_layout_delete);
    mPassButton = new Button(getContext());
    mPassButton.setText(R.string.tinder_stack_layout_pass);
    mApproveButton = new Button(getContext());
    mApproveButton.setText(R.string.tinder_stack_layout_approve);

    mButtonsContainer.addView(mDeleteButton);
    mButtonsContainer.addView(mPassButton);
    mButtonsContainer.addView(mApproveButton);

    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    addView(mCardsContainer, layoutParams);
    addView(mButtonsContainer, layoutParams);
  }

  public void addCard(TinderCardView tinderCardView) {
    View firstCard = mCardsContainer.getChildAt(0);

    if (firstCard != null && firstCard.equals(tinderCardView)) {
      return;
    }

    if (onCardSwipedListener == null)
      onCardSwipedListener = tinderCardView.getOnCardSwipedListener();

    topCardOnStack = tinderCardView;

    ViewGroup.LayoutParams layoutParams;
    layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);

    int childCount = mCardsContainer.getChildCount();
    mCardsContainer.addView(tinderCardView, 0, layoutParams);

    float scaleValue = 1 - (childCount / 50.0f);

    tinderCardView.animate()
        .x(0)
        .y(childCount * yMultiplier)
        .scaleX(scaleValue)
        .setInterpolator(new AnticipateOvershootInterpolator())
        .setDuration(DURATION);
  }

  public TinderCardView getTopCardOnStack() {
    return topCardOnStack;
  }

  @Override
  public void addView(View child, int index, ViewGroup.LayoutParams params) {
    super.addView(child, index, params);
    if (onCardSwipedListener != null)
      onCardSwipedListener.onNext(getChildCount());
  }


  @Override
  public void removeView(View view) {
    super.removeView(view);
    if (onCardSwipedListener != null)
      onCardSwipedListener.onNext(getChildCount());
  }

  @Override
  public void onDetachedFromWindow() {
    super.onDetachedFromWindow();
  }

  public FrameLayout getCardsContainer() {
    return mCardsContainer;
  }

  public LinearLayout getButtonsContainer() {
    return mButtonsContainer;
  }
}

您可以创建一个新类,并从
LinearLayout
扩展,将其方向设置为垂直,然后将
TinderStackLayout
添加到该类中。然后向其中添加另一个
LinearLayout
。还有一件事,您正在为
LinearLayout
使用
RelativeLayout.LayoutParams
,这不起作用。您应该使用
LinearLayout.LayoutParams
ViewGroup.LayoutParams
。谢谢,我会试试。@ShayanD试过了,我遇到了一个问题-我看不到我的按钮容器,在主帖子中添加了编辑