简体   繁体   English

如何以编程方式更改Android ListView的布局边距

[英]how to change the layout margin for an Android ListView Programmatically

I have defined a List View in xml as below 我在xml中定义了一个List View,如下所示

     <ListView android:id="@+id/mylist" 
           android:layout_width="fill_parent" 
           android:layout_height="wrap_content"
            android:cacheColorHint="#00000000"
           android:layout_weight="1"
           android:layout_marginTop="95dip"/>

And i need to re-define the layout margin upon some result in my programe ,how i can achieve this 我需要在我的程序中重新定义布局余量,我可以如何实现这一点

More simply, you may be able to modify the existing MarginLayoutParams instance to modify the margin: 更简单地说,您可以修改现有的MarginLayoutParams实例来修改边距:

ViewGroup.MarginLayoutParams mlp = (ViewGroup.MarginLayoutParams) mListView
        .getLayoutParams();

mlp.setMargins(adjustmentPxs, 0, 0, 0);

break;

如果你在Activity中获得了ListView对象,你可以使用方法setLayoutParams() ,传入android.view.ViewGroup.MarginLayoutParams的实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM