简体   繁体   English

相对布局的布局参数问题

[英]An issue with layout params of relative layout

I am having an issue with my app, basically i have few layouts like this: |ABC| 我的应用出现问题,基本上我的布局很少,例如:| ABC | each letter describe one layout(A is the relative layout). 每个字母描述一个布局(A是相对布局)。 When i am adding an image view to the relative layout, this image view can move over all the layout. 当我向相对布局添加图像视图时,此图像视图可以在所有布局上移动。 But the relative layout became stretched to whole the width of the screen. 但是相对的布局却延伸到整个屏幕的整个宽度。 |A| | A | so basically you can`t see the B,C layouts any more. 所以基本上您无法再看到B,C布局。

This is my function: 这是我的功能:

public void startGame(){
    int i;
    for(i=0;i<currentLevelObs.size();i++){
        if(currentLevelObs.get(i)==Globals.OBS_ENTRY_INDEX)
            break;
    }


    lp = new RelativeLayout.LayoutParams(
                     RelativeLayout.LayoutParams.FILL_PARENT,
                     RelativeLayout.LayoutParams.FILL_PARENT);
    lp.addRule(RelativeLayout.BELOW, (i+1));
    lp.addRule(RelativeLayout.RIGHT_OF, i);
    lp.leftMargin = 25;


    ImageView img = new ImageView(this);
    img.setLayoutParams(lp);
    img.setBackgroundResource(R.drawable.hero);
    gameLayout.addView(img);

}

This function called from onCreate method. 此函数从onCreate方法调用。

You have given layout width to fill parent. 您已指定layout widthfill parent. So it will take up the whole screen. 因此,它将占用整个屏幕。 Thats the problem. 那就是问题所在。 Change it to wrap-content and then check. 将其更改为wrap-content ,然后检查。

Add your ABC layouts to a ScrollView , that way you can scroll down to see B and C . ABC布局添加到ScrollView ,这样您可以向下滚动以查看BC

it should be like this: 应该是这样的:

<ScrollView
..
...
....>
<D
..>
A
B
C
</D>
</ScrollView>

a ScrollView can contain only 1 child, so keep your ABC in a D layout which is in ScrollView ScrollView只能包含1个孩子,因此将ABC保持在ScrollView中的D布局中

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

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