开发者

Adding two views in Android

开发者 https://www.devze.com 2023-03-19 00:10 出处:网络
So what I want to do is use a gridview at the top of my screen and a table view at the bottom half of my screen. I thought this would be easy but apparently I am wrong. And yes I did my usual search f

So what I want to do is use a gridview at the top of my screen and a table view at the bottom half of my screen. I thought this would be easy but apparently I am wrong. And yes I did my usual search for 1 hour then post something on stackoverflow.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" 
android:layout_height="fill_parent"
android:stretchColumns="1">


<GridView 
android:id="@+id/gridview"
android:layout_width="fill_parent" 
android:numColumns="4"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"
android:layout_height="130dp"/>

<TableLayout
android:layout_width="fill_parent" 
android:layout_height="fill_parent"
android:stretchColumns="1">

<TableRow>
    <TextView 
    android:text="Name:" 
    android:id="@+id/TextView01" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"/>


    <EditText 
    android:hint="Name"
    android:id="@+id/projectName" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"/>
</TableRow>

<TableR开发者_StackOverflow社区ow>
    <TextView 
    android:text="Start" 
    android:id="@+id/startText" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"/>


    <Button
    android:text="Push"
    android:id="@+id/startdate" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
    android:onClick="startDate"/>

</TableRow>

<TableRow>
    <TextView 
    android:text="Finish" 
    android:id="@+id/finishText" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"/>


    <Button
    android:id="@+id/finishdate" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
    android:onClick="finishDate"/>
</TableRow>
</TableLayout>

</LinearLayout>

Maybe this is easy. Maybe I should just go to bed and do this in the morning. Thanks for your help.


1) Add android:orientation="vertical" to LinearLayout.

2) Use android:layout_height="fill_parent" for BOTH Grid n Table layout AND also add android:layout_weight="0.5" in both GridLayout and TableLayot. This will equally divide both layouts in screen.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    android:stretchColumns="1" android:orientation="vertical">


    <GridView android:id="@+id/gridview" android:layout_width="fill_parent"
        android:numColumns="4" android:verticalSpacing="10dp"
        android:horizontalSpacing="10dp" android:stretchMode="columnWidth"
        android:gravity="center" **android:layout_height="fill_parent"**
        **android:layout_weight="0.5"** />

    <TableLayout android:layout_width="fill_parent"
        **android:layout_height="fill_parent"** android:stretchColumns="1"
        **android:layout_weight="0.5"**>

Rest part remains same...

You can try different combinations of layout_weight like 0.4 & 0.6 or 0.8 & 0.2 etc if you don't want the layouts equally dividing the screen. But if you use layout_height to some fixed pixel or dip value, it will appear different on different devices screens... So I recommend use layout_weight parameter.


The default orientation for the LinearLayout is horizontal.

Add android:orientation="vertical" to your LinearLayout.

And don't set android:layout_height="fill_parent" to your TableLayout otherwise it takes the full screen.

0

精彩评论

暂无评论...
验证码 换一张
取 消