开发者

Android Widget reserve 3 cells instead of 2 cells in height

开发者 https://www.devze.com 2023-04-02 12:48 出处:网络
i have a strange problem. I changed my Widget Layout to a height of 200dp to reduce the space to 2 Cells on the smartphone. But when i want to place it on the screen it always reserve 3 Cells and plac

i have a strange problem. I changed my Widget Layout to a height of 200dp to reduce the space to 2 Cells on the smartphone. But when i want to place it on the screen it always reserve 3 Cells and place the widget in the middle of the 3 Cells. Width Android 2.3 you can change the size of the Widget out of the System. When i do that i get the Widget to 2 Cells without any layout lost.

Do you have any ideas?

Stefan

//EDIT I added the XML Code

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" 
    android:layout_marginLeft="8dip" 
    android:layout_marginRight="8dip" 
    android:orientation="vertical"
    android:layout_height="200dp">


    <LinearLayout 
    android:layout_width="match_parent" 
    android:id="@+id/linearLayout55" 
    android:layout_height="187dip" 
    android:orientation="vertical"
    android:paddingTop="4dip"
    android:background="@drawable/design">

    <LinearLayout android:layout_width="match_parent" android:id="@+id/linearLayout1" android:layout_height="wrap_content" android:gravity="center_vertical" android:paddingLeft="4dip">
        <ImageView android:layout_width="60dip" android:src="@drawable/icon" android:id="@+id/imageView1" android:layout_height="60dip"></ImageView>

        <LinearLayout android:layout_width="match_parent" android:id="@+id/linearLayout11" android:layout_height="wrap_content" android:gravity="center_vertical" android:orientation="vertical">
            <TextView android:layout_width="wrap_content" android:id="@+id/AppTitel" android:textColor="#3b5091" android:paddingLeft="4dip" android:text="My App" android:textSize="20dip" android:textStyle="bold|italic" android:layout_height="wrap_content"></TextView>
            <TextView android:layout_width="wrap_content" android:id="@+id/AppCopyRight" android:textColor="#FFFFFF" android:paddingLeft="4dip" android:text="dummy text" android:textSize="9dip" android:textStyle="italic" android:layout_height="wrap_content"></TextView>
        </LinearLayout>
    </LinearLayout>

    <LinearLayout android:layout_width="299dip" android:id="@+id/ll_con_1" android:gravity="center_vertical" android:paddingLeft="4dip" android:background="@drawable/item_states" android:layout_height="50dip" android:layout_marginLeft="2dip" android:clickable="true" android:visibility="gone" android:orientation="vertical" android:paddingTop="0dip">
        <TextView android:layout_width="wrap_content" android:textColor="#FFFFFF" android:layout_height="wrap_content" android:id="@+id/Item1" android:text=""></TextView>
        <TextView android:layout_width="wrap_content" android:textColor="#CCCCCC" android:layout_height="wrap_content" android:id="@+id/Item1_msg" android:text=""></TextView>
    </LinearLayout>
    <LinearLayout android:layout_width="299dip" android:id="@+id/ll_con_2" android:gravity="center_vertical" android:paddingLeft="4dip" android:background="@drawable/item_states" android:layout_height="50dip" android:layout_marginLeft="2dip" android:clickable="true" android:visibility="gone" android:orientation="vertical" android:paddingTop="0dip">
        <TextView android:layout_width="wrap_content" android:textColor="#FFFFFF" android:layout_height="wrap_content" android:id="@+id/Item2" android:text=""></TextView>
        <TextView android:layout_width="wrap_content" android:textColor="#CCCCCC" android:layout_height="wrap_content" android:id="@+id/Item2_msg" android:text=""></TextView>
    </LinearLayout>


    </LinearLayout>


    <LinearLayout android:layout_width="match_parent" android:id="@+id/linearLayout56" android:gravity="center_horizontal" android:layout_height="45dip" android:layout_marginTop="-17dip">
        <ImageButton android:layout_height="30dip" android:layout_width="60dip" android:src="@drawable/rolodex" android:id="@+id/BtnMSGs" android:background="@drawable/design_item" android:layout_marginRight="5dip"></ImageButton>
        <ImageButton android:layout_height="30dip" android:layout_width="60dip" android:src="@drawable/pencil" android:id="@+id/BtnNewMSG" android:background="@drawable/design_item"></ImageButton>
    </LinearLayout>


</LinearLa开发者_StackOverflow社区yout>


You should create widget layout in another way. Inside xml folder create widget provider

    <appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
    android:minWidth="294dip"
    android:minHeight="220dip"
    android:updatePeriodMillis="1000000"
    android:initialLayout="@layout/widget"
/>

then inside layout folder create layout without width and height specification

    <?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:orientation="vertical"
    android:layout_gravity="center"
    android:background="@drawable/widget_background"
    android:layout_margin="10dip">
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:layout_weight="3">
        <TextView android:id="@+id/widget_textview"
            android:layout_height="wrap_content"
            android:layout_width="fill_parent"   
            android:gravity="center"
            android:textSize="24dip"
            android:textColor="#0d0d0c"
            android:padding="5dip">
        </TextView>
        <TextView android:id="@+id/widget_textview2"
            android:layout_height="wrap_content"
            android:layout_width="fill_parent"   
            android:paddingLeft="20dip"
            android:paddingRight="20dip"
            android:textColor="#0d0d0c"
            android:textSize="22dip">
        </TextView>
    </LinearLayout>
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="horizontal"
        android:layout_weight="7">
        <TextView android:id="@+id/widget_textdate"
            android:layout_height="wrap_content"
            android:layout_width="fill_parent"   
            android:paddingLeft="20dip"
            android:paddingBottom="10dip"
            android:textColor="#0d0d0c"
            android:textSize="20dip">
        </TextView>
    </LinearLayout>
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center_horizontal"
        android:orientation="horizontal"
        android:layout_weight="7"
        android:paddingTop="5dip">
        <Button
            android:id="@+id/previous_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Poprzednie"
            android:textSize="16dip">
        </Button>
        <Button
            android:id="@+id/refresh_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Odśwież"
            android:textSize="16dip">
        </Button>
        <Button
            android:id="@+id/next_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Następne"
            android:textSize="16dip">
        </Button>
    </LinearLayout>
</LinearLayout>

this is how I did it.


Have you tried setting the height to 146dp?

From the documentation:

Because the Home screen's layout orientation (and thus, the cell sizes) can change,as a rule of thumb, you should assume the worst-case cell size of 74 pixels for the height and width of a cell. However, you must subtract 2 from the final dimension to account for any integer rounding errors that occur in the pixel count. To find your minimum width and height in density-independent pixels (dp), use this formula:

(number of cells * 74) - 2

Following this formula, you should use 72 dp for a height of one cell, 294 dp and for a width of four cells.


As you can read here http://developer.android.com/guide/practices/ui_guidelines/widget_design.html#sizes one cell in height takes 80 pixels and 100 in width so you should change 200 to 160 if you want 2 cells

0

精彩评论

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

关注公众号