【问题标题】:Change Relative layout width and height dynamically动态更改相对布局宽度和高度
【发布时间】:2013-03-15 05:08:24
【问题描述】:

您好,我在 LinearLayout 中使用以下布局结构

<TableLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <TableRow
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >
    </TableRow>

    <TableRow
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/tv1"
            android:layout_height="wrap_content"
            android:gravity="left"
            android:paddingLeft="10dp"
            android:textColor="#000" />

        <TextView
            android:id="@+id/tv2"
            android:layout_height="wrap_content"
            android:layout_weight="1.0"
            android:gravity="right"
            android:paddingRight="10dp"
            android:textColor="#000" />
    </TableRow>
</TableLayout>

<ImageView
    android:id="@+id/img1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center" />

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="320px"
    android:layout_height="320px"
    android:gravity="center" >
</RelativeLayout>

并且想要从 java 文件动态设置相对布局宽度和高度,而不是在 xml 文件中将其设置为 320px 但无法做到这一点,方向更改不是问题,因为我将其限制为仅在纵向模式下。可以通过使用 match_parent 在全屏上设置相对布局,但我必须在屏幕上放置另一个视图,所以有可能还是我必须以另一种方式实现它...

【问题讨论】:

  • 要动态设置布局宽度,您必须从您创建的类扩展 RelativeLayout 类并覆盖 onMeasure 方法
  • 感谢您的回复,我已经尝试了所有方法,当我尝试动态设置宽度和高度时,它给出了类转换异常。相对布局还包含其他子视图作为图像视图,因此我将其作为 Java 文件中的视图组,但无法做到这一点。
  • 请贴出异常及其指向的代码
  • 这是代码.. ViewGroup container = (ViewGroup) this.findViewById(R.id.containerr);我正在以 ViewGroup 的形式访问该相对布局,其中 containerr 是 xml 文件中相对布局的 ID。
  • 如果我将 layoutparams 设置为容器,它会显示错误.. Java.lang.ClassCastException:RelativeLayout&LayoutParams cannot be cast to LinearLayout&LayoutParams

标签: android android-layout


【解决方案1】:

Android 不会在 wrap_content 显示后刷新视图布局。 即使使用invalidate() 或requestLayout()。 所以如果你添加一个子视图,或者动态修改内容,你就完蛋了。

getLayoutParams().height = x 加上requestLayout() 是一个很好的解决方案,如果您知道“x”并且您只需要这样做一次。 之后,LayoutParams 中的 wrap_content 将丢失,因为您已将其覆盖。

为了解决这个问题,我编写了一个静态类,它重新计算大小并强制更新带有wrap_content 的视图的布局。此处提供了代码和使用说明:

https://github.com/ea167/android-layout-wrap-content-updater

享受吧!

【讨论】:

  • 嘿,Eric,您有代码的使用示例吗?我似乎无法访问您班级的方法?!
  • 你用那门课有效地救了我的命。非常感谢。
【解决方案2】:

试试这个

getLayoutParams().height= x;
requestLayout(); or invalidate(); 

【讨论】:

    【解决方案3】:

    在 .xml 中为您的相对布局提供一个 ID:

    android:id="@+id/relativelayout"
    

    现在在你的 java 文件中写下这个:

    RelativeLayout Rl = (RelativeLayout) findViewById(R.id.relativelayout);
    
    RelativeLayout.LayoutParams layout_description = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT or YourUserDefinedwidth,
                            RelativeLayout.LayoutParams.FILL_PARENT or YourUserDefinedHeight);
    
    Rl.setLayoutParams(layout_description);
    

    【讨论】:

      【解决方案4】:

      从下面的代码你可以得到设备的高度和宽度:-

      Display display = getWindowManager().getDefaultDisplay();
          int width = (display.getWidth() );
          int height = (display.getHeight() );
      

      paramsTop 你的真实布局:-

      现在你可以设置你想要的高度或宽度。

              paramsTop = new RelativeLayout.LayoutParams(width, height);
      

      【讨论】:

      【解决方案5】:

      您可以使用 LayoutParams 动态设置视图尺寸。 像这样的:

      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;
      

      【讨论】:

        【解决方案6】:

        我遇到了这个问题,像这样动态更改相对布局宽度和高度的最佳解决方案

        RelativeLayout relMain = (RelativeLayout) convertView.findViewById(R.id.relMain);
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
            params.width =200;
         params.height =200;
         params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
                relMain.setLayoutParams(params);
        

        【讨论】:

        • 如何添加持续时间?
        【解决方案7】:

        如果您打算更改高度,那么这将是一个技巧。

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

        【讨论】:

          【解决方案8】:

          我在使用具有wrap_content 的LinearLayout 和一个具有TextView 的match_parent 的孩子时遇到了同样的问题。

          以编程方式删除TextView,然后重新添加。

          linearLayout.removeView(textView)
          linearLayout.addView(textView)
          

          我知道这听起来很愚蠢,但它确实有效。 在我的情况下,调用 invalidate 不起作用,只有这个起作用。

          根据您的实现,您需要处理其父级中的视图索​​引

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2015-03-31
            • 2015-08-06
            • 1970-01-01
            • 1970-01-01
            • 2016-08-01
            相关资源
            最近更新 更多