should I move from layout_weight to dip?

2019-08-11 05:28发布

I have created a widget. I intentionally used layout_weight

so it would fit any screen size.

The widget is always spanned to 1X4.

I now look at different devices in the graphic editor and I see the weight doesn't help.

should I move to dip size?

The linear-layouts are spanned differently in relation to the background image.

enter image description here

enter image description here

enter image description here

My XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/app_widget_root"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/widget_bg_main"
    android:clickable="true"
    android:focusable="true"
    android:gravity="center_vertical"
    android:orientation="horizontal"
    android:padding="0dp" >

    <RelativeLayout
        android:id="@+id/layout_status_image"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_gravity="center"
        android:gravity="center"
        android:orientation="horizontal" 
android:layout_weight="0.21"
>

        <ImageView
            android:id="@+id/image_status"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:src="@drawable/widget_icon_no_data"
            android:visibility="invisible" />
        <!--
                              Layout is necessary because the setVisibility of ProgressBar is not working 
                              through remote views in 2.1. So wrapped by this layout 
        -->

        <FrameLayout
            android:id="@+android:id/widget_progress"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:layout_marginLeft="1dp"
            android:layout_marginTop="1dp" >

            <ProgressBar
                style="?android:attr/progressBarStyle"
                android:layout_width="39dp"
                android:layout_height="39dp"
                android:indeterminateOnly="true"
                android:orientation="vertical" />
        </FrameLayout>
    </RelativeLayout> <!-- Status image layout -->


    <!--
                            ========================================================================
                            * Information layout - contains all the texts 
                            ========================================================================
    -->

    <LinearLayout
        android:id="@+id/layout_information"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:gravity="center"
        android:orientation="vertical" 
        android:paddingLeft="20dp"
        android:layout_weight="0.56"
        >

        <TextView
            android:id="@+id/text_view_destination"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="\@ Home in"
            android:textColor="@color/solid_white"
            android:textSize="19sp"
            android:textStyle="bold" 
            android:layout_gravity="left" 

            />

        <TextView
            android:id="@+id/text_view_time"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:textColor="@color/solid_white"
            android:textSize="25sp"
            android:textStyle="normal" 
            android:layout_gravity="left"
            />
    </LinearLayout> <!-- Information layout -->
    <!--
                            ========================================================================
                            * Action layout - action buttons container
                            ========================================================================
    -->

    <LinearLayout
        android:id="@+id/layout_action"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_gravity="center"
        android:gravity="center"
        android:orientation="vertical" 
        android:layout_weight="0.23"
        >

        <ImageView
            android:id="@+id/image_action"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:src="@drawable/widget_bt_drive_disabled" />

        <TextView
            android:id="@+id/text_view_action"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="2dp"
            android:text="Drive!"
            android:textColor="@color/disabled_white"
            android:textSize="15sp"
            android:textStyle="bold" 
            android:layout_gravity="center"
            />
    </LinearLayout> <!-- Action layout -->

1条回答
唯我独甜
2楼-- · 2019-08-11 05:46

It is obvious because single layout working perfectly on a normal screen sized device doesn't mean that it'll work the same way on large/xlarge sized screens. So create a folder inside res "layout-large" or "layout-xlarge" (depending on device screen size). Copy same layout into newly created folder and then make some changes to it, so it'll fit for large/xlarge screens as per your requirement. Also you may copy images into ldpi, mdpi, hdpi and xhdpi to support multiple densities.

For more info: http://developer.android.com/guide/practices/screens_support.html

Hope this helps.

查看更多
登录 后发表回答