Android : cant set margin dynamically on android

on Sunday, March 29, 2015


my app contains 3 imageviews and i used a textview as a place holder,here is my layout.xml



<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"
android:background="@layout/back"
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=".SplasActivity" >

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text=" " />

<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:scaleType="fitXY"
android:src="@drawable/list_sher" />

<ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:scaleType="fitXY"
android:src="@drawable/list_shaer" />

<ImageView
android:id="@+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:scaleType="fitXY"
android:src="@drawable/list_fav" />


then i used below codes to change image sizes,it works fine but when i set margins for my textview, everything goes crazy. textview stays but i cant see any imageview !



private void setsize(){
// this codes work fine //
DisplayMetrics dm=new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int w=dm.widthPixels;
int h=dm.heightPixels;

int h2=(h/7);
int w2=w-((w*10)/100);
int h3=(h/20)+1;

LinearLayout.LayoutParams pram=new LinearLayout.LayoutParams(w2,h2);
img1.setLayoutParams(pram);
img2.setLayoutParams(pram);
img3.setLayoutParams(pram);


// the problem is here //
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);

LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);

layoutParams.setMargins(0, h3, 0, 0);
ll.addView(txt1,layoutParams);

}

0 comments:

Post a Comment