6

I have a linear layout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout_menu"
android:layout_width="fill_parent"
android:layout_height="70px"
android:layout_alignParentBottom="true"
android:background="@drawable/footer_bar"
android:gravity="center" >
</LinearLayout>

When i set condition

if (constant.getscreenresolution() >= 800) {
    //i want to make it height * 2
}

So how to set the layout params?

Alan Lai
  • 1,094
  • 7
  • 18
  • 41

5 Answers5

12

For padding:

LinearLayout mLayout = (LinearLayout)findViewById(R.id.layout_menu);
mLayout.setPadding(left, top, right, bottom);
Hanon
  • 3,917
  • 2
  • 25
  • 29
6

hope this helps you

LinearLayout.LayoutParams layout_param= new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.fill_parent,
                height * 2);
mLayout = (LinearLayout) findViewById(R.id.layout_menu);
mLayout.setLayoutParams(layout_param);
MichaelP
  • 2,761
  • 5
  • 31
  • 36
  • 1
    return me this exception `05-28 13:41:11.125: E/AndroidRuntime(16380): java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.RelativeLayout$LayoutParams ` my layout is linear but why involve relative? – Alan Lai May 28 '12 at 05:42
  • Alan, You should use RelativeLayout params instead of LinearLayout params – yeahdixon Mar 11 '15 at 20:02
6
LinearLayout mLayout = (LinearLayout)v.findViewById(R.id.ll_winfowindo);
int left= (int) activity.getResources().getDimension(R.dimen._18sdp);
int top=(int) activity.getResources().getDimension(R.dimen._22sdp);
int right=(int) activity.getResources().getDimension(R.dimen._18sdp);
int bottom=(int) activity.getResources().getDimension(R.dimen._34sdp);
mLayout.setPadding(left, top, right, bottom);
AskNilesh
  • 67,701
  • 16
  • 123
  • 163
0

You shouldn't be doing this. First you're using pixels (px) instead of device independent pixels (dp) Doing things like this create UIs that only work for specific device screen configurations. You need to learn how Android addresses the variations in screen densities so that your app is screen density independent. Start here:

http://developer.android.com/guide/practices/screens_support.html

chubbsondubs
  • 37,646
  • 24
  • 106
  • 138
0

Add Margin to Linear Layout :-

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

layoutParams.setMargins(30, 20, 30, 0);
Manohar
  • 22,116
  • 9
  • 108
  • 144
Sarbjyot
  • 136
  • 2
  • 8