RelativeLayout具有相同的宽度和高度

时间:2022-11-19 14:11:49

I have a list of photos, each having some text in it. I would like each photo to horizontally fill the screen and would like to height to be the same as of the width

我有一张照片列表,每张照片都有一些文字。我希望每张照片都水平填充屏幕,并希望高度与宽度相同

Trying to accomplish something like

试图完成类似的事情

<RelativeLayout
  android:layout_width="match_parent"
  android:layout_height="self.width"
...

How could I accomplish this ?

我怎么能做到这一点?

Since I will be using a recycleview I can edit the height at runtime.

由于我将使用recycleview,我可以在运行时编辑高度。

3 个解决方案

#1


0  

XML is not dynamic. Screen measurements have to be done in Java during onCreate.

XML不是动态的。屏幕测量必须在onCreate期间用Java完成。

 Display display = getWindowManager().getDefaultDisplay();
    Point size = new Point(); display.getSize(size);
    int width=size.x; int height=size.y;

LinearLayout.LayoutParams params= new LinearLayout.LayoutParams(width,width);
yourView.setLayoutParams(params);

#2


0  

find width of your layout programmatically, and set that value as height of your layout.

以编程方式查找布局的宽度,并将该值设置为布局的高度。

//get Width
int width=layout.getWidth();

//set height
layout.getLayoutParams().height=width;

#3


0  

should be like this,if you want height for full screen use match_parent also

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"

/>

#1


0  

XML is not dynamic. Screen measurements have to be done in Java during onCreate.

XML不是动态的。屏幕测量必须在onCreate期间用Java完成。

 Display display = getWindowManager().getDefaultDisplay();
    Point size = new Point(); display.getSize(size);
    int width=size.x; int height=size.y;

LinearLayout.LayoutParams params= new LinearLayout.LayoutParams(width,width);
yourView.setLayoutParams(params);

#2


0  

find width of your layout programmatically, and set that value as height of your layout.

以编程方式查找布局的宽度,并将该值设置为布局的高度。

//get Width
int width=layout.getWidth();

//set height
layout.getLayoutParams().height=width;

#3


0  

should be like this,if you want height for full screen use match_parent also

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"

/>