java - Use LinearLayout to display multiple images -
trying set image on image view stretch image based on screen size , auto fit images doesn't go beyond screen.
my mainactivity:
package com.example.testting; import android.os.bundle; import android.app.activity; import android.view.menu; import android.widget.imageview; import android.widget.linearlayout; import android.widget.relativelayout; import android.widget.relativelayout.layoutparams; public class mainactivity extends activity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); linearlayout ll = (linearlayout) findviewbyid(r.id.lllayout); for(int i=0;i<10;i++) { linearlayout.layoutparams layoutparams = new linearlayout.layoutparams(r.dimen.num_icon, r.dimen.num_icon); imageview ii= new imageview(this); ii.setbackgroundresource(r.drawable.apple); ii.setlayoutparams(layoutparams); ll.addview(ii); } } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.main, menu); return true; } }
my xml:
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" tools:context=".mainactivity" android:id="@+id/lllayout" android:orientation="horizontal" android:gravity="center" > </linearlayout>
my res/values/dimens
file standard phone screen:
<resources> <!-- default screen margins, per android design guidelines. --> <dimen name="activity_horizontal_margin">16dp</dimen> <dimen name="activity_vertical_margin">16dp</dimen> <dimen name="num_icon">55dp</dimen> </resources>
my res/values/dimens
file 7" screen:
<resources> <!-- customize dimensions defined in res/values/dimens.xml (such screen margins) sw600dp devices (e.g. 7" tablets) here. --> <dimen name="num_icon">110dp</dimen> </resources>
my res/values/dimens
file 10" screen:
<resources> <!-- customize dimensions defined in res/values/dimens.xml (such screen margins) sw720dp devices (e.g. 10" tablets) here. --> <dimen name="num_icon">160dp</dimen> </resources>
it supposed display 10 images using dimension xml it's not. seeing following:
1. r.dimen.some_value should contain dimens defined in res/values-xxxx folder.
2. i'm not sure mean adding image in next line, try using relativelayout instead, childs of relativelayout can positioned relative other views using properties android:layout_above, android:layout_below, etc.
Comments
Post a Comment