【问题标题】:Android: How to Programmatically set the size of a LayoutAndroid:如何以编程方式设置布局的大小
【发布时间】:2011-10-11 13:10:30
【问题描述】:

作为 Android 应用程序的一部分,我正在构建一个按钮集。这些按钮是一组嵌套的 LinearLayouts 的一部分。使用权重,我可以根据包含的父 LinearLayout 的大小自动调整自己的大小。这个想法是,根据屏幕的像素数和密度,将包含布局的大小设置为多个像素;并让按钮集根据该更改自行调整大小。

接下来的问题是:如何调整布局大小。

我已经尝试了几种建议的技术,但没有一个可以接近工作。下面是构建按钮集的 XML 子集:

    <LinearLayout android:layout_height="104pt" android:id="@+id/numberPadLayout" android:orientation="horizontal" android:layout_width="104pt"
    android:background="#a0a0a0"
    android:layout_alignParentBottom="true" 
    android:layout_centerHorizontal="true"
>

    <LinearLayout android:layout_weight="2" android:layout_height="fill_parent" android:id="@+id/linearLayout1" android:orientation="vertical" android:layout_width="wrap_content">
        <Button android:text="1" android:id="@+id/button1" android:layout_weight="2" android:layout_width="fill_parent" android:layout_height="wrap_content"></Button>
        <Button android:text="4" android:id="@+id/button4" android:layout_weight="2" android:layout_width="fill_parent" android:layout_height="wrap_content"></Button>
        <Button android:text="7" android:id="@+id/button7" android:layout_weight="2" android:layout_width="fill_parent" android:layout_height="wrap_content"></Button>
        <Button android:text="-" android:id="@+id/buttonDash" android:layout_weight="2" android:layout_width="fill_parent" android:layout_height="wrap_content"></Button>
    </LinearLayout>
    <LinearLayout android:layout_weight="2" android:layout_height="fill_parent" android:id="@+id/linearLayout2" android:orientation="vertical" android:layout_width="wrap_content">
        <Button android:text="2" android:id="@+id/button2" android:layout_weight="2" android:layout_width="fill_parent" android:layout_height="wrap_content"></Button>
        <Button android:text="5" android:id="@+id/button5" android:layout_weight="2" android:layout_width="fill_parent" android:layout_height="wrap_content"></Button>
        <Button android:text="8" android:id="@+id/button8" android:layout_weight="2" android:layout_width="fill_parent" android:layout_height="wrap_content"></Button>
        <Button android:text="0" android:id="@+id/button0" android:layout_weight="2" android:layout_width="fill_parent" android:layout_height="wrap_content"></Button>
    </LinearLayout>

    <LinearLayout android:layout_weight="2" android:layout_height="fill_parent" android:id="@+id/linearLayout3" android:orientation="vertical" android:layout_width="wrap_content">
        <Button android:text="3" android:id="@+id/button3" android:layout_weight="2" android:layout_width="fill_parent" android:layout_height="wrap_content"></Button>
        <Button android:text="6" android:id="@+id/button6" android:layout_weight="2" android:layout_width="fill_parent" android:layout_height="wrap_content"></Button>
        <Button android:text="9" android:id="@+id/button9" android:layout_weight="2" android:layout_width="fill_parent" android:layout_height="wrap_content"></Button>
        <Button android:text="." android:id="@+id/buttonDot" android:layout_weight="2" android:layout_width="fill_parent" android:layout_height="wrap_content"></Button>
    </LinearLayout>
    <LinearLayout android:layout_weight="2" android:layout_height="fill_parent" android:id="@+id/linearLayout4" android:orientation="vertical" android:layout_width="wrap_content">
        <Button android:text="/" android:id="@+id/buttonBack" android:layout_weight="2" android:layout_width="fill_parent" android:layout_height="wrap_content"></Button>
        <Button android:text="\" android:id="@+id/buttonEnter" android:layout_weight="2" android:layout_width="fill_parent" android:layout_height="wrap_content"></Button>
    </LinearLayout>

</LinearLayout>    

这两个问题是:1)如何从 Java 访问 numberPadLayout。一旦我可以访问视图,2) 如何更改布局的高度和宽度。

任何建议将不胜感激。

【问题讨论】:

    标签: android layout resize android-linearlayout


    【解决方案1】:

    Java

    这应该可行:

    // Gets linearlayout
    LinearLayout layout = findViewById(R.id.numberPadLayout);
    // Gets the layout params that will allow you to resize the layout
    LayoutParams params = layout.getLayoutParams();
    // Changes the height and width to the specified *pixels*
    params.height = 100;
    params.width = 100;
    layout.setLayoutParams(params);
    

    如果要将倾角转换为像素,请使用:

    int height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, <HEIGHT>, getResources().getDisplayMetrics());
    

    Kotlin

    【讨论】:

    • 谢谢,这正是我所需要的。我的假设似乎错了,我需要将参数应用回布局才能正常工作。
    • 如果您需要根据其他视图的计算值进行更改,请将此与stackoverflow.com/a/8171014/9648结合使用。
    • 它不起作用。我的布局宽度是2000。当我打印出layout.getWidth()时,即使我输入params.width=1000,它仍然是2000
    • ^ layout.setLayoutParams(params);
    【解决方案2】:
    LinearLayout YOUR_LinearLayout =(LinearLayout)findViewById(R.id.YOUR_LinearLayout)
        LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
                           /*width*/ ViewGroup.LayoutParams.MATCH_PARENT,
                   /*height*/ 100,
                   /*weight*/ 1.0f
                    );
                    YOUR_LinearLayout.setLayoutParams(param);
    

    【讨论】:

      【解决方案3】:

      我的示例代码

      wv = (WebView) findViewById(R.id.mywebview);
      wv.getLayoutParams().height = LayoutParams.MATCH_PARENT; // LayoutParams: android.view.ViewGroup.LayoutParams
      // wv.getLayoutParams().height = LayoutParams.WRAP_CONTENT;
      wv.requestLayout();//It is necesary to refresh the screen
      

      【讨论】:

      • WebView 使用闻起来就像您在 Cordova 应用程序中使用它,对吧?
      • requestLayout() 为我做了!
      【解决方案4】:

      您可以使用以下代码获取被调用布局的实际高度:

      public int getLayoutSize() {
      // Get the layout id
          final LinearLayout root = (LinearLayout) findViewById(R.id.mainroot);
          final AtomicInteger layoutHeight = new AtomicInteger();
      
          root.post(new Runnable() { 
          public void run() { 
              Rect rect = new Rect(); 
              Window win = getWindow();  // Get the Window
                      win.getDecorView().getWindowVisibleDisplayFrame(rect);
      
                      // Get the height of Status Bar
                      int statusBarHeight = rect.top;
      
                      // Get the height occupied by the decoration contents 
                      int contentViewTop = win.findViewById(Window.ID_ANDROID_CONTENT).getTop();
      
                      // Calculate titleBarHeight by deducting statusBarHeight from contentViewTop  
                      int titleBarHeight = contentViewTop - statusBarHeight; 
                      Log.i("MY", "titleHeight = " + titleBarHeight + " statusHeight = " + statusBarHeight + " contentViewTop = " + contentViewTop); 
      
                      // By now we got the height of titleBar & statusBar
                      // Now lets get the screen size
                      DisplayMetrics metrics = new DisplayMetrics();
                      getWindowManager().getDefaultDisplay().getMetrics(metrics);   
                      int screenHeight = metrics.heightPixels;
                      int screenWidth = metrics.widthPixels;
                      Log.i("MY", "Actual Screen Height = " + screenHeight + " Width = " + screenWidth);   
      
                      // Now calculate the height that our layout can be set
                      // If you know that your application doesn't have statusBar added, then don't add here also. Same applies to application bar also 
                      layoutHeight.set(screenHeight - (titleBarHeight + statusBarHeight));
                      Log.i("MY", "Layout Height = " + layoutHeight);   
      
                  // Lastly, set the height of the layout       
                  FrameLayout.LayoutParams rootParams = (FrameLayout.LayoutParams)root.getLayoutParams();
                  rootParams.height = layoutHeight.get();
                  root.setLayoutParams(rootParams);
              }
          });
      
      return layoutHeight.get();
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-11-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多