【问题标题】:how to add android:layout_marginBottom rule in code?如何在代码中添加 android:layout_marginBottom 规则?
【发布时间】:2013-11-11 23:06:44
【问题描述】:

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

【问题讨论】:

    标签: java android layout android-relativelayout


    【解决方案1】:
    layoutParams.bottomMargin=0;//your bottom margin value
    listWhatever.setLayoutParams(layoutParams);
    

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

    【讨论】:

    • 它应该可以工作。也许它是您正在寻找的填充? listWhatever.setPadding(左,上,右,下); (看不到你的评论了,但我不会删除这个:))
    • 它将被接受,因为它正在工作,需要重建,但需要等待 5 分钟,因为如此愚蠢的规则“你可以在 4 分钟内接受答案”
    【解决方案2】:

    试试这个:

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

    【讨论】:

    • 这与 android:layout_marginBottom 不等价,但很高兴:+1
    猜你喜欢
    • 2019-04-26
    • 1970-01-01
    • 2020-01-06
    • 2018-09-22
    • 1970-01-01
    • 2023-03-18
    • 2013-12-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多