Android以编程方式设置边距

我想设置保证金来查看编程。 这是我的XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/llAttendeeList" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:background="#000000"> <ListView android:id="@+id/attendeelistview" android:layout_width="fill_parent" android:layout_height="wrap_content" android:listSelector="#F6CECE" > </ListView> </LinearLayout> 

而当我的buttononClick调出popup窗口视图的方法:

 private void openPopUp() { LayoutInflater layoutInflater = (LayoutInflater) getActivity() .getBaseContext().getSystemService( context.LAYOUT_INFLATER_SERVICE); View popupView = layoutInflater.inflate(R.layout.event_attendee_pop, null); llAttendeeList = (LinearLayout) popupView .findViewById(R.id.llAttendeeList); attendeeListView = (ListView) popupView.findViewById(R.id.attendeelistview); LayoutParams params = new LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT ); params.setMargins(10, 10, 10, 0); popupView.setLayoutParams(params); final PopupWindow popupWindow = new PopupWindow(popupView, LayoutParams.WRAP_CONTENT, 350); popupWindow.setOutsideTouchable(true); popupWindow.setBackgroundDrawable(new BitmapDrawable()); popupWindow.setTouchInterceptor(new OnTouchListener() { public boolean onTouch(View v, MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_OUTSIDE) { popupWindow.dismiss(); return true; } return false; } }); popupWindow.showAtLocation(popupView, Gravity.CENTER, 0, 0); mAdapter = new ListAdapter(getActivity()); attendeeListView.setAdapter(mAdapter); } 

我试图设置marginView到popupView,但没有运气。 保证金不在那里。 我不知道哪个部分覆盖它。

提前致谢。

Related of "Android以编程方式设置边距"

手动设置PopupWindow的宽度和高度:

 Display display = getWindowManager().getDefaultDisplay(); Point size = new Point(); display.getSize(size); int width = size.x; int height = size.y; popupWindow.setWidth(width-10); popupWindow.setHeight(height-10); 

尝试使用布局,它将工作

 LinearLayout.LayoutParams params = new LayoutParams ( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT ); params.setMargins(left, top, right, bottom); yourbutton.setLayoutParams(params); 

按照这个教程http://www.codota.com/android/classes/android.view.ViewGroup.MarginLayoutParams