Monday, 15 June 2015

android - Background drawables define view size -



android - Background drawables define view size -

for sake of clarity, question has been edited significantly.

let's have .png resource of 100x100px:

i want utilize image background textviews, size determined content, variable @ runtime. especially, want image tile when textview size larger 100x100px, , want cropped when textview smaller 100x100px.

the latter seems hard one.

example code

i have drawable resource bg_tile.xml:

<bitmap xmlns:android="http://schemas.android.com/apk/res/android" android:src="@drawable/bg" android:tilemode="repeat" />

my layout file:

<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="8dp" android:background="@drawable/bg_tile" android:text="short text" android:textsize="20sp" /> <textview android:layout_margin="8dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/bg_tile" android:text="long text long text long text long text long text long text long text long text long textlong text long text long text long text long text long text long text long text long textlong text long text long text long text long text long text long text long text long text " android:textsize="20sp" /> </linearlayout> what's happening

here screenshot of result of code, , 1 of expect / want:

as can see, tiling part works, , bottom tile beingness cutting off. however, when textview smaller background, textview takes size of background.

suggested solutions android:gravity="clip_vertical|clip_horizontal" - doesn't work, without tilemode. use match_parent layout params - not option. setting fixed height - there no way of telling how long content of textview be, , size.

how can accomplish goal?

i have not tried myself, seek idea/hack/workaround:

the minimum height/width of view (textview) determined background drawable (amongst other things). uses background drawable's getminimumheight() , getminimumwidth() determine view's minimum height , width.

it looks want view's minimum height , width 0 (excluding padding) , view's width/height should based on view's content (in case, textview's text).

try in oncreate of activity or oncreateview of fragment, hold of textview trying 'fix'. let's phone call textview 'tv':

textview tv; ... ... tv = (textview)...findviewbyid(...); .... // swap 'standard' bitmap background 1 has no minimum width/height. bitmapdrawable background = (bitmapdrawable)tv.getbackground(); // assuming have bg_tile background. bitmapdrawable newbackground = new bitmapdrawable(background.getbitmap) { @override public int getminimumwidth() { homecoming 0; } @override public int getminimumheight() { homecoming 0; } }; newbackground.settilemodexy(background.gettilemodex(), background.gettilemodey()); tv.setbackgrounddrawable(newbackground); ... ...

android

No comments:

Post a Comment