【问题标题】:Android set margin of view programmaticallyAndroid以编程方式设置视图边距
【发布时间】:2015-02-24 04:41:39
【问题描述】:

我想以编程方式将边距设置为查看。这是我的 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调出popup Window视图时的方法:

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 设置边距,但没有成功。边距不存在。我想知道哪个部分覆盖它。

【问题讨论】:

    标签: java android view margin


    【解决方案1】:

    尝试使用布局它会工作

    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

    【讨论】:

    • 我确实用过。您可以查看我发布的问题。
    • 尝试添加LinearLayout.LayoutParams,因为这取决于您使用的布局兄弟..
    • 不,不起作用。它仍然占据了整个屏幕的宽度
    • 点击链接,它会给你灵感
    • @Denise 在创建 PopUpWindow 时给 static 宽度而不是 LayoutParams.WRAP_CONTENT
    【解决方案2】:

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

    【讨论】:

    • @Denise,很高兴为您提供帮助。
    猜你喜欢
    • 2011-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-26
    • 1970-01-01
    • 2011-02-12
    相关资源
    最近更新 更多