开发者

How to manage Layout In android

I had just started working on android. Actually I had created a calculator application.

The problem I have faced is to manage the layout. I have two rows of buttons. In each row are 4 buttons. When I run the application the space between the first row and the second row is quite large. So can anyone tell me how can I decrease the space between these two rows?

The code I have written:

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

    <LinearLayout
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1">


    <TextView
        android:id="@+id/label"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Calculator Application"/>

    <EditText
        android:id="@+id/entry"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@android:drawable/editbox_background"
        android:layout_below="@id/label"/>

        <LinearLayout
                android:orientation="vertical"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="1">
                <LinearLayout
                        android:orientation="horizontal"
                        android:layout_width="fill_parent"
                        android:layout_height="fill_parent"
                        android:layout_weight="1">
                    <Button
                        android:id="@+id/one"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"

                        android:layout_alignParentRight="true"
                        android:layout_marginLeft="10dip"
                        android:text="1" />
                    <Button
                        android:id="@+id/two"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"

                        android:layout_alignParentRight="true"
                        android:layout_marginLeft="10dip"
                        android:text="2" />
                    <Button
                        android:id="@+id/three"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"

                        android:layout_alignParentRight="true"
                        android:layout_marginLeft="10dip"
                        android:text="3" />
                    <Button
                        android:id="@+id/plus"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"

                        android:layout_alignParentRight="true"
                        android:layout_marginLeft="10dip"
                        android:text="+" />

                </LinearLayout>
        </LinearLayout>

        <LinearLayout
                android:orientation="vertical"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="1">
                <LinearLayout
                        android:orientation="horizontal"
                        android:layout_width="fill_parent"
                        android:layout_height="fill_parent"
                        android:layout_weight="1">
                <Button
                android:id="@+id/four"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"

                android:layout_alignParentRight="true"
                android:layout_marginLeft="10dip"
                android:text="4" />
                <Button
                android:id="@+id/five"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"

                android:layout_alignParentRight="true"
                android:layout_marginLeft="10dip"
                android:text="5" />
                <Button
                android:id="@+id/six"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"

                android:layout_alignParentRight="true"
                android:layout_marginLeft="10dip"
                android:text="6" />
                <Button
                android:id="@+id/minus"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"

                android:layout_alignParentRight="true"
                android:layout_marginLeft="10dip"
                android:text="-" />
                </LinearLayout>
        </LinearLayout>

        <LinearLayout
                android:orientation="vertical"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="1">
                <LinearLayout
                        android:orientation="horizontal"
                        android:layout_width="fill_parent"
                        android:layout_height="fill_parent"
                        android:layout_weight="1">
                <Button
                android:id="@+id/seven"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"

                android:layout_alignParentRight="true"
                android:layout_marginLeft="10dip"
                android:text="7" />
                <Button
                android:id="@+id/eight"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"

                android:layout_alignParentRight="true"
                android:layout_marginLeft="10dip"
                android:text="8" />
                <Button
                android:id="@+id/nine"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"

                android:layout_alignParentRight="true"
                android:layout_marginLeft="10dip"
                android:text="9" />
                <Button
                android:id="@+id/multiply"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"

                android:layout_alignParentRight="true"
                android:layout_marginLeft="10dip"
                android:text="*" />
                </LinearLayout>
        </LinearLayout>

        <LinearLayout
                android:orientation="vertical"
                android:layout_width="fill_parent"
        开发者_C百科        android:layout_height="fill_parent"
                android:layout_weight="1">
                <LinearLayout
                        android:orientation="horizontal"
                        android:layout_width="fill_parent"
                        android:layout_height="fill_parent"
                        android:layout_weight="1">
                <Button
                android:id="@+id/zero"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"

                android:layout_alignParentRight="true"
                android:layout_marginLeft="10dip"
                android:text="0" />
                <Button
                android:id="@+id/clr"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"

                android:layout_alignParentRight="true"
                android:layout_marginLeft="10dip"
                android:text="c" />
                <Button
                android:id="@+id/equal"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"

                android:layout_alignParentRight="true"
                android:layout_marginLeft="10dip"
                android:text="=" />
                <Button
                android:id="@+id/divide"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"

                android:layout_alignParentRight="true"
                android:layout_marginLeft="10dip"
                android:text="/" />
                </LinearLayout>
        </LinearLayout>


  </LinearLayout>

</LinearLayout>

Regards

Anshuman


Replace all of the
android:layout_height="fill_parent" calls to
android:layout_height="wrap_content"
(apart from the parent layout, which should be fill_parent)
I have verified that this works.

EDIT: To make the buttons fill the screen horizontally, (and all be equally sized), add this to each button:

android:layout_width="0dp"
android:layout_weight="1"


the better way is u can use grid layout.try this links

http://www.edumobile.org/android/android-beginner-tutorials/draw-menu-in-grid-view/

http://www.firstdroid.com/2011/02/06/android-tutorial-gridview-with-icon-and-text/


As CaspNZ suggested use android:layout_height="wrap_content" also using top padding may help.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜