简体   繁体   English

如何将填充或边距设置为线性布局?

[英]how to set padding or margin to linear layout?

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 ? 那么如何设置layout params

For padding: 用于填充:

LinearLayout mLayout = (LinearLayout)findViewById(R.id.layout_menu);
mLayout.setPadding(left, top, right, bottom);
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);

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);

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. 首先,您使用像素(px)而不是设备无关像素(dp)这样的事情会创建仅适用于特定设备屏幕配置的UI。 You need to learn how Android addresses the variations in screen densities so that your app is screen density independent. 您需要了解Android如何解决屏幕密度的变化,以便您的应用与屏幕密度无关。 Start here: 从这里开始:

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

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM