android - How to add child Views to parent RelativeLayout with percentage of width and height -
i have parent relativelayout want use container hold number of item views. imagine shuffling pack of cards , seeing visual stack of cards, skewed each other, on top of 1 stack. have achieved iterating on collection, inflaying layout xml file template each "card" , populating items details, before adding view parent relativelayout, setting x, y, , rotation properties.
this works, each card same width , height parent relativelayout if not rotated, take entire parent layout. ones rotated, corners disappear out of bounds of relativelayout , cannot see them.
what want each child view have width , height of 90% of parent relativelayout rotating of layered views on stack, can see corners of ones beneath coming out sides. cannot figure out how though.
this parent relativelayout
<relativelayout android:id="@+id/layoutview" android:layout_alignparenttop="true" android:layout_centerhorizontal="true" android:layout_above="@id/tayle_buttons" android:layout_height="wrap_content" android:layout_width="fill_parent" android:background="#005500"/>
this oncreateview() method of fragment
view rootview = inflater.inflate(r.layout.fragment_tayleresults, container, false); windowwidth = m_windowmanager.getdefaultdisplay().getwidth(); screencenter = windowwidth / 2; parentview = (relativelayout) rootview.findviewbyid(r.id.layoutview);
and code populates each child view (unnecessary code removed)
final view m_view = inflater.inflate(r.layout.fragment_tayle, null); relativelayout m_toplayout = (relativelayout) m_view.findviewbyid(r.id.t_layout); //populate view control values, eg textviews, imageviews here m_view.setx(0); m_view.sety(0); m_view.settag(i); if (i == 0) { m_view.setrotation(0); } else if (i == 1) { m_view.setrotation(-5); } else if (i == 2) { m_view.setrotation(3); } else if (i == 3) { m_view.setrotation(7); } else if (i == 4) { m_view.setrotation(-2); } else if (i == 5) { m_view.setrotation(5); } parentview.addview(m_view);
lastly, child layout each "card"
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/t_layout" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="4dip" android:weightsum="2" android:background="@drawable/tayle_border" > <imageview android:id="@+id/genre" android:layout_height="60dp" android:layout_width="50dp" android:layout_alignparenttop="true" android:layout_alignparentright="true" /> ...and on
for knowing parent's width , height can implement view tree observer. code modified snippet here:
viewtreeobserver viewtreeobserver = relativelayout.getviewtreeobserver(); if (viewtreeobserver.isalive()) { viewtreeobserver.addongloballayoutlistener(new ongloballayoutlistener() { @override public void ongloballayout() { view.getviewtreeobserver().removeongloballayoutlistener(this); viewwidth = relativelayout.getwidth(); viewheight = relativelayout.getheight(); } }); }
now have width , height of view in pixels. can scale imageview accordingly. here relativelayout.getwidth() returns pixels calling if before or during layout-pass return 0. viewtreeobserver used inorder mitigate it.
Comments
Post a Comment