【问题标题】:How to create layout withminimum and maximum height?如何创建最小和最大高度的布局?
【发布时间】:2014-06-14 06:29:00
【问题描述】:

我有两个布局 layout1 和 layout2 采用垂直重力的线性布局。
我的标准 layout1 高度例如是 300dp(小显示器的大高度)。

在小型显示器中,这几乎占据了所有视图高度。但我想将 layout1 的最大高度设为视图的 50%。

如果我在大显示器中将高度设置为所有高度视图的 50%,我会有一些损失空间。

如果我在小型显示器中设置高度 300dp,我只有 layout1。

所以我必须限制 layout1 的高度,小显示器的重量限制为 50%,大显示器的 dpi 限制为 300dp。
我怎样才能为我的 layout1 设置这个限制。

【问题讨论】:

    标签: android xml android-layout user-interface android-linearlayout


    【解决方案1】:

    在运行时,您可以确定您的 LinearLayout1 占用了多少空间,并且仅当它占据屏幕的一半以上时才调整其高度。为此,请使用以下代码。

    假设你给 LinearLayout1 的 id 为 R.id.LL1,然后使用这段代码,你可以要求 android 将 LL1 的高度调整为 50%

    int screenHeight, screenWidth;
    
    //Code to determine screen's height and width.
    
    Display display = getWindowManager.getDefaultDisplay();
    if (android.os.Build.Version.SDK_INT>=13) {
        Point size = new Point();
        display.getSize(size);
        screenHeight = size.x;
        screenWidth = size.y;
    }
    else {
        screenWidth = display.getWidth();
        screenHeight = display.getHeight();
    }
    
    
    LinearLayout ll = (LinearLayout) findViewById(R.id.LL1);
    
    int layoutHeight = ll.getLayoutParams().height; // gets layout's height
    
    if ((layoutHeight * 2) > screenHeight) { 
    
        // true when LL1 takes more than half of screen, good time to set it back to 50% height
    
        ll.getLayoutParams().height = screenHeight/2;
        ll.requestLayout(); //Forces layout to be adjusted.
    }
    
    else {
    //All good, no work required, so skip this else block
    }
    

    【讨论】:

    • 您好,感谢您的关注。什么是 getWindowManager?它是一个库,我应该在我的项目中添加一些东西吗?当我使用你的代码时,eclipse 无法识别。
    • @max 引用 this。这是 Activity 类提供的一种方法,用于处理(android 的:P)窗口
    【解决方案2】:

    你知道体重吗?

    如果您想要 2 个布局 (L1 50%) (L2 50%) 高度,请使用:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context=".MainActivity"
        android:weightSum="1"
        >
            <LinearLayout 
            android:orientation="horizontal"
            android:layout_height="0dp"
            android:layout_weight=".5"
            android:layout_width="match_parent"
             >
        </LinearLayout>
    
            <LinearLayout 
            android:orientation="horizontal"
            android:layout_height="0dp"
            android:layout_weight=".5"
            android:layout_width="match_parent"
             >
        </LinearLayout>
    </LinearLayout>
    

    如果您希望屏幕尺寸(大、小)不同,请创建另一个具有相同名称但权重不同的 xml。

    示例:res/layout-large/activity_main.xmlres/layout-small/activity_main.xml 仅更改权重。

    更多信息:http://developer.android.com/guide/practices/screens_support.html

    【讨论】:

      【解决方案3】:

      我通过比较 ScrollView 的高度和屏幕宽度解决了这个问题(可以与高度比较 - 根据您的要求):

      if (myScrollView.getHeight > ((int)(screenWidth * 0.8))) {
      
          myScrollView.getLayoutParams().height = (int)(screenWidth * 0.85);
      
      }
      

      实际上我正在动态添加视图。一旦 ScrollView 的高度达到一定的限制,高度就固定了。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-02-15
        • 1970-01-01
        • 2010-09-06
        • 1970-01-01
        • 2014-10-10
        • 1970-01-01
        • 2012-11-03
        • 1970-01-01
        相关资源
        最近更新 更多