2016-01-13 87 views
0

我有一個PopupWindow它最終決定遵循我的命令,並在空錯誤後顯示自己(當然不是我忘記初始化某些值的錯誤)。Android - 編程方式更改彈出窗口的佈局

下面是該PopupWindow

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/popup_handphone_MainLayout" 
    android:layout_width="match_parent" android:layout_height="match_parent"> 

    <RelativeLayout 
     android:id="@+id/popup_handphone_Wrapper" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:background="@drawable/popupkodehp"> 

     <TextView 
      android:id="@+id/popup_handphone_text" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="10dp" 
      android:gravity="center" 
      android:textColor="@color/textColor" 
      android:text="@string/popupPhoneMessage"/> 

     <RelativeLayout 
      android:id="@+id/popup_handphone_functionalities" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/popup_handphone_text" 
      android:layout_marginTop="15dp"> 

      <EditText 
       android:id="@+id/popup_handphone_phoneNumber" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:hint="Phone Number" 
       android:textColorHint="@color/textColor" /> 

      <EditText 
       android:id="@+id/popup_handphone_phoneNumberConfirm" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_below="@+id/popup_handphone_phoneNumber" 
       android:layout_marginTop="10dp" 
       android:hint="Retype Phone Number" 
       android:textColorHint="@color/textColor"/> 

      <Button 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_below="@+id/popup_handphone_phoneNumberConfirm" 
       android:layout_marginTop="20dp" 
       android:minHeight="20dp" 
       android:text="Send Code" 
       android:textColor="@color/textColor"/> 

     </RelativeLayout> 

    </RelativeLayout> 

</RelativeLayout> 

XML文件下面是PopupWindow我都對谷歌

View.OnClickListener phoneReinputHandler = new View.OnClickListener() { 

     public void onClick(View arg0) { 
      /*Intent intent = new Intent(SignupStepTwoActivity.this, PopupHandphone.class); 
      backDim = (RelativeLayout) findViewById(R.id.bac_dim_layout); 
      //backDim.setVisibility(View.VISIBLE); 
      startActivity(intent);*/ 

      mainLayout = (RelativeLayout)findViewById(R.id.activity_signup_step_two_mainLayout); 

      int width = displayMetrics.widthPixels; 
      int height = displayMetrics.heightPixels; 
      inflater = (LayoutInflater)getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
/*   View popupLayoutInflater = inflater.inflate(R.layout.popup_handphone, mainLayout); 

      RelativeLayout popupFunctionalitiesWrapper = (RelativeLayout)popupLayoutInflater.findViewById(R.id.popup_handphone_functionalities); 
      int popupFunctionalitiesWrapperHeight = layoutResize.height(70, displayMetrics); 
      RelativeLayout.LayoutParams popupFunctionalitiesWrapperParams = (RelativeLayout.LayoutParams)popupFunctionalitiesWrapper.getLayoutParams(); 
      popupFunctionalitiesWrapperParams.height = popupFunctionalitiesWrapperHeight; 
*/ 
      PopupWindow pw = new PopupWindow(
        inflater.inflate(R.layout.popup_handphone, null, false), 
        (int)(width * .8), 
        (int)(height*.35), 
        true); 
      pw.setBackgroundDrawable(new ColorDrawable(getResources().getColor(android.R.color.transparent))); 
      pw.setOutsideTouchable(true); 

      pw.showAtLocation(mainLayout, Gravity.CENTER, 0, 0); 

     } 
    }; 

匿名和讓人歎爲觀止的幫助下取得的第二個代碼註釋部分是未能嘗試設置佈局元素的高度。它也顯示在主佈局上,因爲整個PopupWindow佈局都位於班級的佈局之上。

如何以編程方式自定義我從班級佈局製作的佈局PopupWindow

回答

0

我已經找到了如何使用popupWindow.getContentView()

像往常一樣來改變PopupWindow的內部答案,實例化PopupWindow

PopupWindow pw = new PopupWindow(
        inflater.inflate(R.layout.popup_handphone, null, false), 
        (int)(width * .8), 
        (int)(height*.35), 
        true); 

使用然後調用PopupWindow佈局..

RelativeLayout popupFunctionalitiesWrapper = (RelativeLayout)pw.getContentView().findViewById(R.id.popup_handphone_functionalities); 

請注意,pw.getContentView()之前有findViewById

和通說popupFunctionalitiesWrapper,我可以設置的東西如..

int popupFunctionalitiesWrapperWidth = layoutResize.width(70); 
      RelativeLayout.LayoutParams popupFunctionalitiesWrapperParams = (RelativeLayout.LayoutParams)popupFunctionalitiesWrapper.getLayoutParams(); 
      popupFunctionalitiesWrapperParams.width = popupFunctionalitiesWrapperWidth; 
      popupFunctionalitiesWrapperParams.addRule(Gravity.CENTER); 
      popupFunctionalitiesWrapper.setLayoutParams(popupFunctionalitiesWrapperParams); 

沒錯,就是這樣!

0

如果要在顯示popupwindow時更新寬度和高度PopupWindow,請使用函數PopupWindow.update

希望這會有所幫助。

+0

這是窗戶本身嗎?或者它可以用於內部的單個組件? –

+0

如果你想重置'width'和'height'然後'PopupWindow'對象,或者如果你想更新裏面的單個組件然後通過'PopupWindow.getContentView'從popupwindow獲得視圖,然後更新 –

相關問題