【问题标题】:RelativeLayout to have the same width and heightRelativeLayout 具有相同的宽度和高度
【发布时间】:2015-09-03 12:54:18
【问题描述】:

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

试图完成类似的事情

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

我怎样才能做到这一点?

由于我将使用回收视图,因此我可以在运行时编辑高度。

【问题讨论】:

  • 不能用xml来做
  • 通过调整大小(并丢失纵横比)还是通过裁剪?此外,还不清楚每个元素是否只是一个 ImageView 或一个 ImageView 和一个 TextView(“每个元素中都有一些文本”是什么意思?)

标签: android android-layout android-relativelayout


【解决方案1】:

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】:

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

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

    【讨论】:

      【解决方案3】:
      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"
      
      />
      

      【讨论】:

        猜你喜欢
        • 2017-03-31
        • 2015-11-27
        • 2021-12-15
        • 2019-03-05
        • 1970-01-01
        • 2019-12-11
        • 1970-01-01
        • 2012-12-12
        • 1970-01-01
        相关资源
        最近更新 更多