Setting programmatically the pixel size of a remot

2019-09-04 07:49发布

问题:

Let's say that I have a layout that contains

<ImageView
        android:id="@+id/my_id"
        android:layout_width="123px"
        android:layout_height="345px"
        android:padding="0dip"
        android:scaleType="fitXY" />

which is used in an AppWidget and is set via a RemoteViews. How can I programmatically set the width and height in pixels (overriding the 123 and 345 in the above xml)?

(motivation: this image view shows a dynamic png file that is set at runtime via a file URL. If the layout_width and layout_height above are set to wrap_content, the image is displayed scaled down the the device's density(). Setting the exact width and height in pixels solves the problem but the file size changes and I don't know how to set it programmatically).

回答1:

You can do it in a different way. Put your ImageView in a LinerLayout and set layout_width and layout_height of ImageView to "fill_parent". Then change LinearLayout's width and height.

I assume you know about app widgets size rules.