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

时间:2012-05-28 03:44:40

标签: android layoutparams

我有一个线性布局

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

当我设定条件时

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

那么如何设置layout params

5 个答案:

答案 0 :(得分:10)

填充:

LinearLayout mLayout = (LinearLayout)findViewById(R.id.layout_menu);
mLayout.setPadding(left, top, right, bottom);

答案 1 :(得分:7)

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

答案 2 :(得分:4)

希望这可以帮助你

LinearLayout.LayoutParams layout_param= new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.fill_parent,
                height * 2);
mLayout = (LinearLayout) findViewById(R.id.layout_menu);
mLayout.setLayoutParams(layout_param);

答案 3 :(得分:0)

你不应该这样做。首先,您使用像素(px)而不是设备无关像素(dp)这样的事情会创建仅适用于特定设备屏幕配置的UI。您需要了解Android如何解决屏幕密度的变化,以便您的应用与屏幕密度无关。从这里开始:

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

答案 4 :(得分:0)

将边距添加到线性布局: -

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

layoutParams.setMargins(30, 20, 30, 0);