Android以编程方式设置视图边距

时间:2014-12-27 04:41:22

标签: java android view margin

我想以编程方式将边距设置为View。这是我的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>

当我的按钮onClick调出弹出窗口视图时的方法:

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);
}

我尝试将边距设置为popupView但没有运气。保证金不存在。我想知道哪个部分覆盖了它。

2 个答案:

答案 0 :(得分:5)

手动设置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);

答案 1 :(得分:2)

尝试使用布局,它将起作用

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

进行操作
相关问题