Pergunta

I have two layout layout1 and layout2 in linear layout with vertically gravity.
standard layout1 height for me is for example 300dp (big height for small displays).

In small displays this takes almost all of the view height. But I want to take layout1 maximum height 50% of the view.

If I set height 50% of all height view in big displays I have some loss space.
enter image description here
If I set height 300dp in small displays I just have layout1.
enter image description here
So I have to limit for layout1 height, 50% for weight in small displays and 300dp for dpi in large displays.
how can I set this limits to my layout1.

Foi útil?

Solução

At runtime, you can determine how much space your LinearLayout1 is taking up, and adjust its height only if it takes more than half of the screen. To do this, use following code.

Suppose you have given your LinearLayout1 the id as R.id.LL1, then using this code, you can ask android to adjust height of LL1 to 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
}

Outras dicas

do you know about weigth?

If you want 2 Layouts (L1 50%) (L2 50%) height use this:

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

and if you want diferents % in screen size(large,small), create another xml with the same name but different weight for each one.

Example: res/layout-large/activity_main.xml or res/layout-small/activity_main.xml change only the weight.

More information: http://developer.android.com/guide/practices/screens_support.html

I resolved this issue by comparing ScrollView's height with Screen Width(Can Compare with Height - Based on your requirement) :

if (myScrollView.getHeight > ((int)(screenWidth * 0.8))) {

    myScrollView.getLayoutParams().height = (int)(screenWidth * 0.85);

}

Actually i'm adding views dynamically. Once ScrollView's height reached to certain limit fixed the height.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top