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

6

我有一个线性布局

<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个回答

12

对于填充:

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

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

6
希望这能对你有所帮助。
LinearLayout.LayoutParams layout_param= new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.fill_parent,
                height * 2);
mLayout = (LinearLayout) findViewById(R.id.layout_menu);
mLayout.setLayoutParams(layout_param);

1
请将此异常返回给我 05-28 13:41:11.125: E/AndroidRuntime(16380): java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.RelativeLayout$LayoutParams 我的布局是线性的,为什么会涉及到相对布局呢? - Alan Lai
Alan,你应该使用RelativeLayout参数而不是LinearLayout参数。 - yeahdixon

0

为线性布局添加边距:

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

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

0

你不应该这样做。首先,你正在使用像素(px)而不是设备独立像素(dp)。这样做会创建仅适用于特定设备屏幕配置的用户界面。你需要学习Android如何处理屏幕密度的变化,以使你的应用程序具有屏幕密度独立性。从这里开始:

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


我知道,这就是为什么我创建了两套布局的原因。 - Alan Lai

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接