开发者

how to change the layout margin for an Android ListView Programmatically

开发者 https://www.devze.com 2023-01-04 07:03 出处:网络
I have defined a List View in xml as below <ListView android:id=\"@+id/mylist\" android:layout_width=\"f开发者_StackOverflow社区ill_parent\"

I have defined a List View in xml as below

     <ListView android:id="@+id/mylist" 
           android:layout_width="f开发者_StackOverflow社区ill_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:

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

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

break;


If you get hold of the ListView object in your Activity you can use the method setLayoutParams(), passing in an instance of android.view.ViewGroup.MarginLayoutParams.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号