繁体   English   中英

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

[英]Android set margin of view programmatically

我想以编程方式将边距设置为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);
}

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM