Android:setMargin到ListView

时间:2016-02-02 13:34:08

标签: android listview

我希望设置保证金为listview,但运行应用程序崩溃后!

我的代码:

final ListView lstContent = (ListView) findViewById(R.id.lstContent);
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) lstContent.getLayoutParams();
layoutParams.setMargins(0, 0, 0, 100);
lstContent.setLayoutParams(layoutParams);

这是布局的xml代码

  

XML:

<LinearLayout
                android:id="@+id/lnrArtistChange"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="#272727"
                android:gravity="left|center"
                android:padding="11dp" android:visibility="gone">
                <ListView
                    android:id="@+id/lstContent"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" android:clickable="@null" android:smoothScrollbar="true" android:animationCache="false" android:scrollingCache="false" android:divider="@null" android:visibility="gone" android:layout_marginTop="12dp" android:background="@null" android:scrollbars="none">

                </ListView>

            </LinearLayout>

1 个答案:

答案 0 :(得分:1)

您可以显示 Logcat

您可以尝试使用

      ListView lstContent = (ListView) findViewById(R.id.lstContent);   
      RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
      params.setMargins(0, 0, 0, 100); //left,top,right,bottom
      lstContent.setLayoutParams(params);

您可以查看 how to change the layout margin for an Android ListView Programmatically

希望这会对你有所帮助。