android RelativeLayout 以编程方式更改高度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26058051/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:58:04  来源:igfitidea点击:

android RelativeLayout change height programmatically

androidandroid-scrollviewandroid-relativelayout

提问by Beka

I try change layout height programmatically. I have two buttons. In one button I change my layout position and second button I try to receive save position witch I was first time.

我尝试以编程方式更改布局高度。我有两个按钮。在一个按钮中,我更改了布局位置,在第二个按钮中,我尝试接收我第一次使用的保存位置。

<RelativeLayout
    android:id="@+id/popaplayout"
    android:layout_width="fill_parent"
    android:layout_height="200dp" >

This is a my layout and in first button I wrote

这是我的布局,在我写的第一个按钮中

RelativeLayout.LayoutParams parms = new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.FILL_PARENT,
                    RelativeLayout.LayoutParams.FILL_PARENT);
parms.addRule(RelativeLayout.BELOW, R.id.movies_title_layout);
scrollpopap.setLayoutParams(parms);
scrollpopap.setScrollingEnabled(true);

and second button

和第二个按钮

RelativeLayout.LayoutParams rel_btn = new RelativeLayout.LayoutParams(
                    ViewGroup.LayoutParams.WRAP_CONTENT, 300);
rel_btn.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
scrollpopap.setLayoutParams(rel_btn);
scrollpopap.setScrollingEnabled(false);

In xmlfile I wrote height 200dp and programmatically 300 and there are different height. How I can wrote save height in programmatically?

xml文件中我写了高度 200dp 和编程 300 并且有不同的高度。我如何以编程方式编写保存高度?

if anyone knows solution please help me thanks

如果有人知道解决方案请帮助我谢谢

回答by My God

First convert 200 dpinto pixels -

首先将200 dp转换为像素 -

final float scale = getContext().getResources().getDisplayMetrics().density;
int pixels = (int) (200 * scale + 0.5f);

Then set programatically,

然后以编程方式设置,

RelativeLayout.LayoutParams rel_btn = new RelativeLayout.LayoutParams(
                    ViewGroup.LayoutParams.WRAP_CONTENT, pixels);
            rel_btn.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
            scrollpopap.setLayoutParams(rel_btn);
            scrollpopap.setScrollingEnabled(false);

回答by Ankit Kumar

Convert DP to PX like this.

像这样将DP转换为PX。

final float scale = getContext().getResources().getDisplayMetrics().density;
int px = (int) (100 * scale + 0.5f);  // replace 100 with your dimensions

Set Some layout_gravity of relative layout. and Change height and width by java code.

设置相对布局的一些 layout_gravity。并通过java代码更改高度和宽度。

RelativeLayout rl = (RelativeLayout) findViewById(R.id.yourId);
rl.getLayoutParams().height = px;  
rl.getLayoutParams().width = px;

回答by shaiban

If you intending to change RelativeLayoutheight, then this help.

如果您打算更改RelativeLayout高度,那么这会有所帮助。

RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.yourId);
relativeLayout.getLayoutParams().height = 100;
relativeLayout.getLayoutParams().width = 100;

回答by Ramesh Devaiya

Use this

用这个

RelativeLayout layout = (RelativeLayout) findViewById(R.id.yourlayout_id);
// Gets the layout params that will allow you to resize the layout
LayoutParams params = layout.getLayoutParams();
params.height = 320;
params.width = 320;