how to set padding or margin to linear layout?
Asked Answered
F

5

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?

Farland answered 28/5, 2012 at 3:44 Comment(0)
G
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);
Gayegayel answered 28/5, 2012 at 3:48 Comment(2)
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?Farland
Alan, You should use RelativeLayout params instead of LinearLayout paramsAmmonal
N
12

For padding:

LinearLayout mLayout = (LinearLayout)findViewById(R.id.layout_menu);
mLayout.setPadding(left, top, right, bottom);
Narcissus answered 28/5, 2012 at 3:51 Comment(0)
G
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);
Gayegayel answered 28/5, 2012 at 3:48 Comment(2)
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?Farland
Alan, You should use RelativeLayout params instead of LinearLayout paramsAmmonal
P
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);
Pontine answered 1/8, 2016 at 5:37 Comment(0)
S
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

Sarsenet answered 28/5, 2012 at 3:52 Comment(1)
i know, that why i had created 2 sets of layoutFarland
G
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);
Groovy answered 1/8, 2016 at 5:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.