如何以编程方式更改 Android ListView 的布局边距
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3061579/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to change the layout margin for an Android ListView Programmatically
提问by ganesh
I have defined a List View in xml as below
我在 xml 中定义了一个列表视图,如下所示
<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
我需要根据我的程序中的某些结果重新定义布局边距,我如何实现这一目标
采纳答案by James
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.
如果您在 Activity 中获得 ListView 对象,则可以使用setLayoutParams()方法,传入android.view.ViewGroup.MarginLayoutParams的实例。
回答by Danny Armstrong
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;