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

Android 如何将填充或边距设置为线性布局?,android,layoutparams,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/f

我有一个线性布局

<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>
那么如何设置布局参数呢?

希望这对您有所帮助

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

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

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


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

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

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

我知道,为什么我创建了两套layoutreturn给我这个异常
05-28 13:41:11.125:E/AndroidRuntime(16380):java.lang.ClassCastException:android.widget.LinearLayout$LayoutParams不能转换为android.widget.RelativeLayout$LayoutParams
我的布局是线性的,但为什么涉及relative?Alan,您应该使用RelativeLayout参数而不是LinearLayout参数
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);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
     LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);

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