如何在代码中添加android:layout_marginBottom规则?

时间:2013-10-31 19:28:57

标签: java android layout relativelayout

layout.xml代码部分:

<ListView
    android:id="@+android:id/listWhatever"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_below="@id/rlSomething" 
    android:layout_marginBottom="I need this to set in code dinamically!"/>

Java代码部分:

listWhatever = (ListView)findViewById(R.id.listWhatever);
RelativeLayout.LayoutParams layoutParams = (LayoutParams) listWhatever.getLayoutParams();

layoutParams.addRule(android:layout_marginBottom ... , how?);

2 个答案:

答案 0 :(得分:5)

layoutParams.bottomMargin=0;//your bottom margin value
listWhatever.setLayoutParams(layoutParams);

您可以将RelativeLayout./insert static constant/与addRule一起使用

答案 1 :(得分:1)

试试这个:

 LayoutParams lp= (LayoutParams) list.getLayoutParams();
 lp.setMargins(left, top, right, bottom);
 list.setLayoutParams(lp);