English 中文(简体)
PopupWindow,当大小不明时
原标题:PopupWindow out of screen when size is unspecified
  • 时间:2011-10-08 10:38:39
  •  标签:
  • android

大部分实例都具体指明了人口窗口的宽度和高度。 我希望他们成为EPAAP_CONTENT——因为内容是分明确定的,因此,在构造I中,就湿度和高度设定了2个,并通过showAsDropDown(意见封)展示。

这样做,人口总是低于固定观点,这意味着可以筛选。 The following snippet show the problem. 点击最后一批书状,你发现任何PopupWindow,因为它在窗户外面显示。 为什么不工作? 我指出,明确指明层面(例如200 100)不会引发这一问题。 Try it yourself

package com.zybnet.example.popupdemo;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

public class PopupDemoActivity extends Activity implements OnClickListener {
    private PopupWindow popup;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // -2 means WRAP_CONTENT THIS TRIGGERS THE PROBLEM
        popup = new PopupWindow(getPopupContent(), -2, -2);
        // When you specify the dimensions everything goes fine
        //popup = new PopupWindow(getPopupContent(), 200, 100);

        LinearLayout layout = new LinearLayout(this);
        layout.setOrientation(LinearLayout.VERTICAL);

        // FILL_PARENT  and same layout weight for all children
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(-1, -1, 1);
        for (int i = 0; i < 10; i++) {
            TextView tv = new TextView(this);
            tv.setText("Click to show popup");
            tv.setOnClickListener(this);
            layout.addView(tv, params);
        }
        setContentView(layout);
    }

    @Override
    public void onClick(View view) {
        popup.dismiss();
        popup.showAsDropDown(view);
    }

    private View getPopupContent() {
        TextView popupContent = new TextView(this);
        popupContent.setText("Some text here");
        popupContent.setTextColor(Color.parseColor("#5000ae"));
        popupContent.setBackgroundColor(Color.parseColor("#ff00ff"));
        popupContent.setPadding(10, 20, 20, 10);
        return popupContent;
    }
}
问题回答

I m starting to doubt that in the context of a PopupWindow that -2, -2 actually means WRAP_CONTENT rather I think that its just interpreting it as width = -2 height = -2

来源:

public PopupWindow(View contentView, int width, int height, boolean focusable) {
    if (contentView != null) {
        mContext = contentView.getContext();
        mWindowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
    }
    setContentView(contentView);
    setWidth(width);
    setHeight(height);
    setFocusable(focusable);
}

www.un.org/Depts/DGACM/index_spanish.htm Width = width; 我认为,你所期待的是setWindowLayoutMode (int widthSpec, int highSpec) 。 这正是你应当在<代码>上通过的情况。 EPAAP_CONTENT

如果其他所有因素都失败,则按预期使用<条码>的宽度和高度。

http://www.ohchr.org。

仅凭本人的尝试,加上这一法典,使之发挥作用。

    // -2 means WRAP_CONTENT THIS TRIGGERS THE PROBLEM
    popup = new PopupWindow(getPopupContent(), 200, 100);
    popup.setWindowLayoutMode(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    // When you specify the dimensions everything goes fine
    //popup = new PopupWindow(getPopupContent(), 200, 100);

    LinearLayout layout = new LinearLayout(this);
    layout.setOrientation(LinearLayout.VERTICAL);

我离开了那里原来的评论,以便你能够看到一切应该到哪里去。

Edit 2: Okay, so I took your code you posted and verbatim tried it on my device, and you were right, it didn t work because when Android is determining which views to layout to put it in, it relies on the width and height you provided. What that means is since you are using 0 and 0 as your width and height, Android will come along and say, "Oh look, the box is only a 0 by 0 box so I can display it as a popup below the content." This isn t what is desired though! The thing is, you know that the box is going to be bigger than that, so lets start putting in some different numbers and seeing the results that come from it.

popup = new PopupWindow(getPopupContent(), 1, 1);

Now when I go and click the bottom box, notice that it jumps above. (See screenshot) That s because Android KNOWS that the width and height are 1 (as I set in the constructor) and that the available screen space below the list item is 0. Well, if there s not enough room to display it there, then it must above then!

enter image description here

但等待! 如果像我的目前例子一样,插图每次会增加内容? 情况确实令人感兴趣。 你在我的下一次屏幕上看到,尽管由于距离现在长6条线,人口仍然充斥在底层! Oh crap, right! 这是因为它是根据<代码>1测量的。 我在建筑商中使用。 当时有什么解决办法?

“entergraph

Solution One: My preferred way would be to take a guess at what the average max height of the TextView would be and then simply toss in one generically big number.

popup = new PopupWindow(getPopupContent(), 300, 300); //just guessing it won t get bigger than that

解决办法二: 这更合适,但你却花了一点速度。 采用<代码>Paint的类别,在显示人口之前,测量案文内容大小,并将之并入<代码>setWidth(和setHala(<>。 我走过前面,并建造了一个 几乎<>-em>的完全解决办法,但我在dding子和 st子方面却采取了一定措施(见评论)。

private int maxWidth;
private int maxHeight;
private Paint p = new Paint();
private Rect bounds = new Rect();
private View getPopupContent() {
    maxWidth = 0;
    maxHeight = 0;
    TextView popupContent = new TextView(this);
    popupContent.setText(popupText += "
" + DEMO);
    popupContent.setTextColor(Color.parseColor("#5000ae"));
    popupContent.setBackgroundColor(Color.parseColor("#ff00ff"));
    popupContent.setPadding(10, 20, 20, 10);
    //the measure can only work line by line so I split it up by line
    String[] temp = popupText.split("
");
    for (String s : temp){
        //measure each line of string and get its width and height
        p.getTextBounds(s, 0, s.length(), bounds);

        //keep a running total of the longest width
        maxWidth = (bounds.width() > maxWidth) ? bounds.width() : maxWidth;
        //add up each of the heights
        maxHeight += bounds.height();
    }

    //also take in account the padding too... if you REALLY want it to be completely robust
    //probably adding another 20 or 30 to the maxHeight should be good
    return popupContent;
}

之后在<代码>onClick()中。 我增加了这两条。

    popup.setHeight(maxHeight);
    popup.setWidth(maxWidth);

我在人口内容观点中用<条码>测量(<>>>>>>>方法解决了这一问题:

View content = getPopupContent();

content.measure(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
int measuredHeight = content.getMeasuredHeight();

popup = new PopupWindow(content, ViewGroup.LayoutParams.WRAP_CONTENT, measuredHeight);
final PopupWindow popupWindow = new PopupWindow();
// inflate your layout or dynamically add view
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.alert_reply_chat,null);

popupWindow.setFocusable(true);
popupWindow.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
popupWindow.setHeight(180);
popupWindow.setBackgroundDrawable(null);

popupWindow.setClippingEnabled(false);

popupWindow.setTouchable(true);
popupWindow.setContentView(view);

return popupWindow;

劳动法典是检查的。





相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签