简体   繁体   English

Android-动态布局在主布局中的位置

[英]Android - Position Relative layout within main layout Dynamicaly

I am trying to add Relative Layout to my mainLayout (which is also a relative layout) dynamically. 我正在尝试将相对布局动态添加到我的mainLayout(这也是一个相对布局)中。 I have it so that you can add the layout multiple times by clicking a button. 我有它,因此您可以通过单击一个按钮多次添加布局。 But I want it so that the first layout added is on the leftside of the screen, the second layout is in the middle and the thirs layout is on the right, etc... I did this using the modulus operator but it isnt working. 但是我想要这样,以便添加的第一个布局在屏幕的左侧,第二个布局在中间,而第三个布局在右侧,依此类推...我使用模数运算符进行了此操作,但它不起作用。

Below is my code 下面是我的代码

LinearLayout mLinearLayout;
RelativeLayout rlcopy;
RelativeLayout[] rArray = new RelativeLayout[20];
int counter = 1;
RelativeLayout llcustomrow;
RelativeLayout.LayoutParams relativeLayoutParams; 

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    mLinearLayout = (LinearLayout) inflater.inflate(
            R.layout.customworkout, container, false);

    llcustomrow = (RelativeLayout)mLinearLayout.findViewById(R.id.llcustomrow);

    for(int i = 1;i<rArray.length-1;i++){
        rArray[i] = (RelativeLayout)View.inflate(getActivity(), R.layout.addworkoutlayout, null); 

        if(i%3 == 1 ){
            rArray[i].setGravity(Gravity.LEFT);
        }else if(i%3 == 2){
            rArray[i].setGravity(Gravity.CENTER_HORIZONTAL);
        }else{
            rArray[i].setGravity(Gravity.RIGHT);
        }

    }

    Button bAdd = (Button) mLinearLayout.findViewById(R.id.bAddExcercise);



    bAdd.setOnClickListener(new OnClickListener() {         
        @Override
        public void onClick(View v) {
             llcustomrow.addView(rArray[counter]);  
             counter++;
        }
    });


    return mLinearLayout;
}

I believe the problem is where i am setting the gravity, I do not think it is doing anything 我认为问题出在我设定重力的位置,我认为它没有做任何事情

Why do not you want to use the fragments for these purposes? 您为什么不希望将这些片段用于这些目的? You can use three frames with weight 1, for example, in LinearLayout with horizontal orientation in your main layout file. 您可以在主布局文件中使用权重为1的三个框架,例如,在LinearLayout中水平放置。 Then create three layouts for each frame. 然后为每个框架创建三个布局。 For dynamic work with previous you can use FragmentManager and FragmentTransaction. 对于以前的动态工作,可以使用FragmentManager和FragmentTransaction。 Read about them please. 请阅读有关它们的信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM