Android LinearLayout set layout_gravity in java code -
i have problem on layout :(
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/cuerpo" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:orientation="vertical" > ...
in java code
linearlayout cuerpo = (linearlayout) findviewbyid(r.id.cuerpo); if(align){ cuerpo.setgravity(gravity.center_vertical); }else{ cuerpo.setgravity(gravity.no_gravity); }
i need layout_gravity no gravity.
the layout gravity talking within layoutparams
of of linearlayout
object. so, logically, have follow next steps:
get layoutparams
of linearlayout
.
if know layoutparams
supposed be, can cast them (beware, based on your choice, must create sure correct!).
set gravity
of layoutparams
desire.
so in code be:
viewgroup.layoutparams layoutparams = cuerpo.getlayoutparams(); // step 1. linearlayout.layoutparams castlayoutparams = (linearlayout.layoutparams) layoutparams; // step 2. castlayoutparams.gravity = gravity.center_vertical; // step 3.
or simple 1 liner steps in one:
((linearlayout.layoutparams) cuerpo.getlayoutparams()).gravity = gravity.center_vertical;
please beware:
i cannot see trying do, not seem correct. type of layoutparams
not type of view
, type of layout
view
reside in.
(i.e. in @gincsait's reference, if have button
in linearlayout
, layoutparams
of button
of type linearlayout.layoutparams
. same cannot said layoutparams
of linearlayout
.)
java android layout layout-gravity
No comments:
Post a Comment