2015-05-02 155 views
7

我有一個視圖,我在其中創建了一個PopupWindow。在這PopupWindow,我有一個文本框(EditText)。當點擊編輯文本時,鍵盤會打開並且整個視圖都會上升,但視圖的頂部會順利地從下移動到的動作欄視圖中。到現在爲止還挺好。Android-將視圖添加到佈局導致佈局在ActionBar前面

我現在又增加了一個空GridView沒有任何進一步的配置到PopupWindow,而現在同樣的情況發生,但鑑於整個頂部去在操作欄視圖的頂部和不流暢(彈出變一個混亂,當鍵盤消失彈出的位置變化幾秒鐘,並有一秒從鍵盤「剩菜」)。它也發生在ListView

我想讓視圖的頂部順利地進入「ActionBar」,就像它應該的那樣。

任何想法?

更新:通過以編程方式添加GridView,我在一些設備上部分解決了這個問題,但在某些設備上它仍然發生。

顯示popupwindow

pView=inflater.inflate(R.layout.activity_packbuild, (ViewGroup)mainActivity.findViewById(R.layout.activity_main)); 

pw = new PopupWindow(
      pView, 
      ma.f.getView().getMeasuredWidth(), 
      ma.f.getView().getMeasuredHeight(), 
      true); 

pw.showAtLocation(mainActivity.getWindow().getDecorView().findViewById(android.R.id.content), Gravity.BOTTOM, 0, 0); 

GOOD:

enter image description here

BAD:

enter image description here

佈局:

<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:id="@+id/lID"> 

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/rID" 
    android:background="@drawable/back5"> 


    <ImageView 
     android:layout_width="125px" 
     android:layout_height="200px" 
     android:id="@+id/mImageView" 
     android:background="@drawable/com_facebook_picker_default_separator_color" 
     android:layout_alignParentTop="true" 
     android:layout_weight="1" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="300dp" /> 

    <EditText 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/editText" 
     android:layout_below="@+id/mImageView" 
     android:layout_marginTop="22dp" 
     android:layout_toRightOf="@+id/lButton" 
     android:layout_toLeftOf="@+id/rButton" /> 

    <ImageView 
     android:layout_width="125px" 
     android:layout_height="200px" 
     android:id="@+id/imageViewRR" 
     android:background="@drawable/com_facebook_picker_default_separator_color" 
     android:layout_alignTop="@+id/mImageView" 
     android:layout_toRightOf="@+id/mImageView" 
     android:layout_marginLeft="20dp" /> 

    <ImageView 
     android:layout_width="125px" 
     android:layout_height="200px" 
     android:id="@+id/imageViewLL" 
     android:background="@drawable/com_facebook_picker_default_separator_color" 
     android:layout_alignTop="@+id/mImageView" 
     android:layout_toLeftOf="@+id/mImageView" 
     android:layout_marginRight="20dp" /> 

    <ImageButton 
     android:layout_width="40dp" 
     android:layout_height="40dp" 
     android:id="@+id/lButton" 
     android:layout_below="@+id/editText" 
     android:layout_alignLeft="@+id/imageViewLL" 
     android:layout_alignStart="@+id/imageViewLL" /> 

    <ImageButton 
     android:layout_width="40dp" 
     android:layout_height="40dp" 
     android:id="@+id/rButton" 
     android:layout_alignTop="@+id/lButton" 
     android:layout_alignRight="@+id/imageViewRR" 
     android:layout_alignEnd="@+id/imageViewRR" 
     android:layout_marginTop="5dp" /> 

    <ImageButton 
     android:layout_width="40dp" 
     android:layout_height="40dp" 
     android:id="@+id/picButton" 
     android:layout_alignTop="@+id/galleryButton" 
     android:layout_alignLeft="@+id/editText" 
     android:layout_alignStart="@+id/editText" 
     android:background="@drawable/camera2" /> 

    <ImageButton 
     android:layout_width="40dp" 
     android:layout_height="40dp" 
     android:id="@+id/videoButton" 
     android:background="@drawable/video" 
     android:layout_alignTop="@+id/galleryButton" 
     android:layout_centerHorizontal="true" /> 

    <ImageView 
     android:layout_width="125px" 
     android:layout_height="200px" 
     android:id="@+id/imageViewR" 
     android:background="@drawable/com_facebook_picker_default_separator_color" 
     android:layout_above="@+id/editText" 
     android:layout_alignRight="@+id/sendButon" 
     android:layout_alignEnd="@+id/sendButon" /> 

    <ImageView 
     android:layout_width="125px" 
     android:layout_height="200px" 
     android:id="@+id/imageViewL" 
     android:background="@drawable/com_facebook_picker_default_separator_color" 
     android:layout_alignTop="@+id/mImageView" 
     android:layout_alignLeft="@+id/editText" /> 

    <ImageButton 
     android:layout_width="40dp" 
     android:layout_height="40dp" 
     android:id="@+id/galleryButton" 
     android:background="@drawable/gallery" 
     android:layout_alignTop="@+id/rButton" 
     android:layout_alignRight="@+id/imageViewR" /> 

    <ImageButton 
     android:layout_width="100px" 
     android:layout_height="100px" 
     android:id="@+id/toButton" 
     android:layout_above="@+id/imageViewR" 
     android:layout_alignLeft="@+id/picButton" 
     android:layout_alignStart="@+id/imageViewL" 
     android:background="@drawable/fbfriends2" 
     android:layout_alignRight="@+id/imageViewLL" 
     android:layout_alignEnd="@+id/imageViewLL" 
     android:layout_marginBottom="20dp" /> 

    <ImageButton 
     android:layout_width="40dp" 
     android:layout_height="40dp" 
     android:id="@+id/sendButon" 
     android:background="@drawable/go" 
     android:layout_alignTop="@+id/toButton" 
     android:layout_alignRight="@+id/editText" 
     android:layout_alignEnd="@+id/editText" /> 

    <ImageButton 
     android:layout_width="40dp" 
     android:layout_height="40dp" 
     android:id="@+id/plusButtonL" 
     android:background="@drawable/plusp" 
     android:layout_alignTop="@+id/videoButton" 
     android:layout_toRightOf="@+id/imageViewLL" /> 

    <ImageButton 
     android:layout_width="40dp" 
     android:layout_height="40dp" 
     android:id="@+id/minusButtonR" 
     android:background="@drawable/minusp" 
     android:layout_alignTop="@+id/videoButton" 
     android:layout_toLeftOf="@+id/imageViewRR" /> 

    <ImageButton 
     android:layout_width="40dp" 
     android:layout_height="40dp" 
     android:id="@+id/plusButton" 
     android:background="@drawable/plusp" 
     android:layout_alignTop="@+id/videoButton" 
     android:layout_centerHorizontal="true" /> 

    <ImageButton 
     android:layout_width="40dp" 
     android:layout_height="40dp" 
     android:id="@+id/minusButton" 
     android:background="@drawable/minusp" 
     android:layout_alignTop="@+id/videoButton" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="300dp" /> 

</RelativeLayout> 

我加入了GridView編程:

gv=new GridView(ma); 
      List<Student> hhh=new ArrayList<Student>(); 
      if (FriendPickerApplication.selectedUsers!=null) { 
       if (FriendPickerApplication.selectedUsers.size() > 0) { 
        for (GraphUser gu : FriendPickerApplication.selectedUsers) { 
         hhh.add(new Student("name", gu.getId())); 
        } 
       } 
      } 

      StudentAdapter sta=new StudentAdapter(ma,hhh); 

      //gv=(GridView)pView.findViewById(R.id.gridView2); 
      gv.setVisibility(View.VISIBLE); 
      MainActivity.wantedPicSize=ma.screenWidth/12; //6 
      int wantedSize=MainActivity.wantedPicSize+2; //10 

      RelativeLayout.LayoutParams relativeParams = new RelativeLayout.LayoutParams(
        RelativeLayout.LayoutParams.MATCH_PARENT, 
        RelativeLayout.LayoutParams.MATCH_PARENT); 
      relativeParams.addRule(RelativeLayout.LEFT_OF, btnSend.getId()); 
      relativeParams.addRule(RelativeLayout.RIGHT_OF,picFriends.getId()); 
      relativeParams.addRule(RelativeLayout.ALIGN_TOP, picFriends.getId()); 
      relativeParams.addRule(RelativeLayout.ALIGN_BOTTOM, picFriends.getId()); 
      gv.setLayoutParams(relativeParams); 
      gv.setPadding(5,0,5,0); 
      ((RelativeLayout)pView.findViewById(R.id.rID)).addView(gv); 

      ViewGroup.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
        RelativeLayout.LayoutParams.FILL_PARENT, 
        RelativeLayout.LayoutParams.FILL_PARENT);//gv.getLayoutParams(); 

      relativeParams.height = wantedSize+(int)(wantedSize/5); //this is in pixels 
      relativeParams.width = (int)(ma.screenWidth/3.0);//gv.getLayoutParams().width; //(int)((((ma.screenWidth*3)/4)/wantedSize)*wantedSize); 

      gv.setColumnWidth(wantedSize); //+width betwen profiles 
      gv.setVerticalSpacing(5); 
      //gv.setNumColumns((int)((screenWidth/2)/100)); 
      gv.setNumColumns((relativeParams.width/wantedSize)); 
      System.out.print("COLUMNSSSSSSSSSS- "+(relativeParams.width/wantedSize)); 
      gv.setLayoutParams(relativeParams); 
      gv.setVisibility(View.VISIBLE); 

      gv.setAdapter(sta); 
+0

請問您可以張貼一些截圖嗎?我試圖想象如何解決問題,如果可能的話,請在你的'popupwindow'工作的地方發佈一個問題(在某些設備上,如你所說),另一個不在那裏 – TomTsagk

+0

我添加了好的和壞的截圖。 – user2162550

+0

你能否提供你的佈局?也可以使用Dialog/DialogFragment更好 –

回答

0

我同意Pasquale Anatriello發佈的答案。儘管美國可以 你可以嘗試不同的方法:

mPopUpWindow.showAsDropDown(anchor); 
mPopUpWindow.showAsDropDown(anchor, xoff, yoff); 
mPopUpWindow.showAtLocation(parent, gravity, x, y); 

我建議你應該讓彈出只操作欄後得出,以y軸的偏移和重力頂部,說:Y偏移相當於動作條的高度,如果你有行動條〜高度×設備高度的10%,[layout_weight = 1.0]。 即使沒有工作出來,創建一個自定義的視圖[說線性佈局]出現和消失,並像彈出一個窗口,並給你靈活性,因爲你想,你自己處理點擊,使它像一個切換,看代碼動畫::

Animation fadeInAnimation = AnimationUtils.loadAnimation(v.getContext(), android.R.anim.fade_in); 
     fadeInAnimation.setDuration(10); 
     v.startAnimation(fadeInAnimation); 

    Animation fadeOutAnimation = AnimationUtils.loadAnimation(v.getContext(), android.R.anim.fade_out); 
     fadeOutAnimation.setDuration(10); 
     v.startAnimation(fadeOutAnimation); 
1

你這裏的問題是彈出窗口。 由於操作欄顯示在您正在創建的新窗口下方的另一個窗口中,因此無法將其放到操作欄下。此外,使用彈出窗口進行復雜佈局永遠不會有效執行。你有沒有想過將自己的地圖和佈局放在自定義佈局的彈出窗口中?這可能是更容易的解決方案

+0

'爲複雜佈局使用彈出窗口永遠不會有效執行' - 爲什麼? – user2162550