2

I have the following code which is written programmatically,

Display display = this.WindowManager.DefaultDisplay;
Point size = new Point();
int height = size.Y;

bViewLayout.Height = (int)(height * 0.5);

However, I wonder how I could achieve half of the screen in Xml rather than hard coded as follows?

<GridLayout
    android:layout_columnSpan="1"
    android:layout_gravity="fill"
    android:layout_rowSpan="1"
    android:layout_height="135"/>

EDITED: The following code did not work:

   <?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:columnCount="2"
    android:rowCount="2">
    <GridLayout
        android:id="@+id/aView"
        android:layout_columnSpan="1"
        android:layout_gravity="fill"
        android:layout_rowSpan="2"
        android:layout_width="100"
        android:layout_height="25"
        android:text="1" />
    <GridLayout
        android:id="@+id/bView"
        android:layout_columnSpan="1"
        android:layout_gravity="fill"
        android:layout_rowSpan="1"
        android:layout_height="0dp"
        android:layout_width="match_parent"
        android:layout_weight="0.5"
        android:text="2" />
    <GridLayout
        android:id="@+id/cView"
        android:layout_columnSpan="1"
        android:layout_gravity="fill"
        android:layout_rowSpan="1"
        android:layout_height="0dp"
        android:layout_width="match_parent"
        android:layout_weight="0.5"
        android:text="3" />
</GridLayout>
mystackoverflow
  • 357
  • 1
  • 4
  • 10

3 Answers3

5

That would do (for width):

<GridLayout
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="0.5" />

This means "the width is undefined, height is, and I said the weight is 50%" (0.5 out of 1.0 by default), so Android will extends that width to full 50% of the device's screen's width.

For the opposite, switch height value to 0dp and width to something. I'm sure you got it now ;)

shkschneider
  • 17,833
  • 13
  • 59
  • 112
2

Try this, it is a tricky solution, but very effective:

It consist in a relativeLayout with a fake view in the middle and the gridview above!

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/black">
    <View 
        android:id="@+id/fakeView"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_centerVertical="true"/>
    <GridLayout
        android:layout_columnSpan="1"
        android:layout_gravity="fill"
        android:layout_rowSpan="1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/fakeView"
        android:background="@color/white"/>
</RelativeLayout>
1

If I were you my root layout would be a vertical LinearLayout and I would use the weightSum attribute.

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="2"
android:gravity="center"
android:orientation="vertical" >

   <Your_first_half_layout_type
    android:layout_width="match_parent"
    android:layout_height="0dp"
    layout_weight="1"
    />

    <Your_second_half_layout_type
    android:layout_width="match_parent"
    android:layout_height="0dp"
    layout_weight="1" />
</LinearLayout>
karvoynistas
  • 1,295
  • 1
  • 14
  • 30