问题
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
?
回答1:
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);
回答2:
For padding:
LinearLayout mLayout = (LinearLayout)findViewById(R.id.layout_menu);
mLayout.setPadding(left, top, right, bottom);
回答3:
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);
回答4:
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
回答5:
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);
来源:https://stackoverflow.com/questions/10779115/how-to-set-padding-or-margin-to-linear-layout