How to put up or down a view programatically?

时光怂恿深爱的人放手 提交于 2020-01-16 18:33:32

问题


I have a xml that the basic "skeleton" is

<RelativeLayout>
    <FrameLayout>
        <RelativeLayout>
            <TextView>
            </TextView>
            <TextView>
            </TextView>
        </RelativeLayout>
        <DrawView>
            //The view that changes 1
        </DrawVIew>
        <EditText>
            //The view that changes 2
        </EditText>
    </FrameLayout>
    <RelativeLayout>
    </RelativeLayout>
</RelativeLayout>

What I want is that using code the DrawView goes up or down, so sometimes you can use the DrawView and in other moments you can use the EditText, but ever the two views are showed.

How can I do that?


回答1:


Since you are placing both widgets inside a FrameLayout you can change its gravity like below:

FrameLayout.LayoutParams drawViewLayoutParams = drawView.getLayoutParams();
drawViewLayoutParams.gravity = Gravity.BOTTOM;

FrameLayout.LayoutParams editTextLayoutParams = editText.getLayoutParams();
editTextLayoutParams.gravity = Gravity.TOP;



回答2:


The only way to do this is to remove all of the FrameLayout's children and put them back in the order you need.

Or you can use another RelativeLayout and change the LayoutParams for the two Views



来源:https://stackoverflow.com/questions/15997992/how-to-put-up-or-down-a-view-programatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!