开发者

Linear Layout for a calculator app

I am having a problem on the last row of my linear layout. I want the 0 to be the same size as all the other numbers, and then the other 4 buttons in that row to be like below (this is using Absolute Layout):

Correct Look I could not post images so this is a tinypic

But using Linear Layout, which is开发者_如何学Go what I need to use so it looks correct on all screen sizes, I cannot get the last row to look right. The problem is the images, they do not let me scale the buttons correctly. I'm sure I could fix it by scaling down the images, but I want them to stay that size, and just make the buttons thinner. Does anyone know how to do that? Weights don't seem to work.

Needs changing I could not post images so this is a tinypic

Here is my xml file:

<?xml version="1.0" encoding="utf-8"?>

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

    <TextView
        android:id="@+id/widget86"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"/>

    <TextView
        android:id="@+id/widget40"
        android:gravity="right"
        android:textSize="40sp"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"/>

</LinearLayout>

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

    <Button
        android:id="@+id/widget34"
        android:text="7"
        android:gravity="center_vertical|center_horizontal" 
        android:layout_width="wrap_content"
        android:textSize="35sp"
        android:layout_height="fill_parent"
        android:layout_weight="1"/>

    <Button
        android:id="@+id/widget35"
        android:gravity="center_vertical|center_horizontal" 
        android:text="8"
        android:textSize="35sp"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_weight="1"/>

    <Button
        android:id="@+id/widget37"
        android:gravity="center_vertical|center_horizontal" 
        android:text="9"
        android:textSize="35sp"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_weight="1"/>

</LinearLayout>

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

    <Button
        android:id="@+id/widget31"
        android:gravity="center_vertical|center_horizontal" 
        android:text="4"
        android:layout_width="wrap_content"
        android:textSize="35sp"
        android:layout_height="fill_parent"
        android:layout_weight="1"/>

    <Button
        android:id="@+id/widget32"
        android:gravity="center_vertical|center_horizontal" 
        android:text="5"
        android:textSize="35sp"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_weight="1"/>

    <Button
        android:id="@+id/widget33"
        android:gravity="center_vertical|center_horizontal" 
        android:text="6"
        android:textSize="35sp"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_weight="1"/>

</LinearLayout>

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

    <Button
        android:id="@+id/widget28"
        android:gravity="center_vertical|center_horizontal" 
        android:text="1"
        android:textSize="35sp"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_weight="1"/>

    <Button
        android:id="@+id/widget29"
        android:gravity="center_vertical|center_horizontal" 
        android:text="2"
        android:layout_width="wrap_content"
        android:textSize="35sp"
        android:layout_height="fill_parent"
        android:layout_weight="1"/>

    <Button
        android:id="@+id/widget30"
        android:gravity="center_vertical|center_horizontal" 
        android:text="3"
        android:textSize="35sp"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_weight="1"/>

</LinearLayout>

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

    <Button
        android:id="@+id/widget38"
        android:gravity="center_vertical|center_horizontal" 
        android:text="0"
        android:layout_width="wrap_content"
        android:textSize="35sp"
        android:layout_height="fill_parent"
        android:layout_weight="30"/>

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_weight="30">

        <Button
            android:id="@+id/widget72"
                android:gravity="center_vertical|center_horizontal" 
                android:text="R"
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:textSize="35sp"
                android:layout_weight="1"/>

        <Button
                android:id="@+id/widget73"
                android:gravity="center_vertical|center_horizontal" 
                android:text="Z"
                android:layout_width="wrap_content"
                android:textSize="35sp"
                android:layout_height="fill_parent"
                android:layout_weight="1"/>

            <Button
                android:id="@+id/widget70"
                android:gravity="center_vertical|center_horizontal" 
                android:layout_width="wrap_content"
                android:drawableBottom="@drawable/back"
                android:textSize="35sp"
                android:layout_height="fill_parent"
                android:layout_weight="1"/>

            <Button
                android:id="@+id/widget39"
                android:gravity="center_vertical|center_horizontal" 
                android:textSize="35sp"
                android:drawableBottom="@drawable/search"
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:layout_weight="1"/>

     </LinearLayout>

</LinearLayout>


Last LinearLayout consists of two objects: Button and LinearLayout. You want it to be divided: 1 unit for button and 2 units for LL. So you should set button layout_weight="1" and LL layout_weight="2". Set also layout_width="fill_parent" (or "0dp" - should act the same as weight will provide proper weight).

You can also put "R" and "Z" to one LL and "arrow", "search" to second LL. Then you will have three objects the same size so put layout_weight="1" to each of them.

Here is article about using weight parameter: http://developer.android.com/resources/articles/layout-tricks-efficiency.html


The last line is clearly running out of space. Try using a TableLayout or a RelativeLayout. But using those also, you will probably have space problems.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜