12

我updgraded appcompat v7版本23.0.123.1.0AppCompatDialogFragment問題與程序兼容性-V7:23.1.0

版本,我發現了關於這一驚喜AppCompatDialogFragment
額外的空間,在顯示出來對話框頂部

其他人正在遇到這種情況?

enter image description here

這裏我的代碼:

public class AlertDialogFragment extends AppCompatDialogFragment { 

    public static final int ALERTDIALOG_STARTUP = 101; 
    public static final int ALERTDIALOG_DELETE_SEARCH_HISTORY = 102; 
    public static final int ALERTDIALOG_RATE_APP = 103; 
    public static final int ALERTDIALOG_REACHED_FOLLOWS_FREE_LIMIT = 104; 
    public static final int ALERTDIALOG_UNFOLLOW_ROUTE = 105; 
    AlertDialogListener mListener; 

    public static AlertDialogFragment newInstance(int type, String id, String[] params) { 
     AlertDialogFragment frag = new AlertDialogFragment(); 
     Bundle args = new Bundle(); 
     args.putInt("type", type); 
     args.putString("id", id); 
     args.putStringArray("params", params); 
     frag.setArguments(args); 
     return frag; 
    } 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
    } 

    @Override 
    public void onAttach(Context activity) { 
     super.onAttach(activity); 
     try { 
      mListener = (AlertDialogListener) activity; 
     } catch (ClassCastException e) { 
      throw new ClassCastException(activity.toString() + " must implement AlertDialogListener"); 
     } 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
    { 
     View view = inflater.inflate(R.layout.view_alertdialog, container, false); 
     int type = getArguments().getInt("type"); 
     String[] params = getArguments().getStringArray("params"); 
     switch (type) { 
      case ALERTDIALOG_STARTUP: 
       setCancelable(false); 
       view = setupView(view, getString(R.string.startup_title), getString(R.string.startup_message), getString(R.string.go_to_facebook), getString(R.string.go_to_app)); 
       break; 
      case ALERTDIALOG_DELETE_SEARCH_HISTORY: 
       view = setupView(view, params[0], getString(R.string.remove_search_message), getString(android.R.string.yes), getString(android.R.string.no)); 
       break; 
      case ALERTDIALOG_RATE_APP: 
       setCancelable(false); 
       view = setupView(view, getString(R.string.rate_app_title), getString(R.string.rate_app_message), getString(R.string.rate_app_yes), getString(R.string.rate_app_no)); 
       break; 
      case ALERTDIALOG_REACHED_FOLLOWS_FREE_LIMIT: 
       view = setupView(view, params[0], params[1], params[2], null); 
       break; 
      case ALERTDIALOG_UNFOLLOW_ROUTE: 
       view = setupView(view, params[0], getString(R.string.unfollow_route_message), getString(android.R.string.yes), getString(android.R.string.no)); 
       break; 
     } 
     return view; 
    } 

    private View setupView(View view, String title, String text, String positiveButtonLabel, String negativeButtonLabel) { 
     TextView customTitle = (TextView) view.findViewById(R.id.custom_title); 
     customTitle.setText(title); 
     TextView customMessage = (TextView) view.findViewById(R.id.custom_message); 
     customMessage.setText(Html.fromHtml(text)); 

     if (positiveButtonLabel!=null) { 
      Button positiveButton = (Button) view.findViewById(R.id.positive_button); 
      positiveButton.setVisibility(View.VISIBLE); 
      positiveButton.setText(positiveButtonLabel); 
      positiveButton.setOnClickListener(new View.OnClickListener() { 
       public void onClick(View v) { 
        mListener.onAlertDialogPositiveClick(getArguments().getInt("type"), getArguments().getString("id")); 
        dismiss(); 
       } 
      }); 
     } 

     if (negativeButtonLabel!=null) { 
      Button negativeButton = (Button) view.findViewById(R.id.negative_button); 
      negativeButton.setVisibility(View.VISIBLE); 
      negativeButton.setText(negativeButtonLabel); 
      negativeButton.setOnClickListener(new View.OnClickListener() { 
       public void onClick(View v) { 
        mListener.onAlertDialogNegativeClick(getArguments().getInt("type"), getArguments().getString("id")); 
        dismiss(); 
       } 
      }); 
     } 
     return view; 
    } 

    public interface AlertDialogListener { 
     public void onAlertDialogPositiveClick(int dialogType, String id); 
     public void onAlertDialogNegativeClick(int dialogType, String id); 
    } 
} 

這裏是view_alertdialog.xml佈局文件:

<LinearLayout 
    android:orientation="vertical" 
    android:paddingTop="@dimen/alert_dialog_padding_top" 
    android:paddingLeft="@dimen/alert_dialog_padding_hor" 
    android:paddingRight="@dimen/alert_dialog_padding_hor" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"> 

    <TextView 
     android:id="@+id/custom_title" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:textColor="@color/evidence_color" 
     android:textStyle="bold" 
     android:textSize="@dimen/alert_dialog_title_textsize" /> 

    <TextView 
     android:id="@+id/custom_message" 
     android:layout_marginTop="@dimen/about_activity_paragraph_marginTop" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:textColor="@color/top_color" 
     android:textSize="@dimen/alert_dialog_message_textsize" /> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:gravity="right" 
     style="?attr/buttonBarStyle"> 

     <Button 
      android:id="@+id/negative_button" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@android:string/cancel" 
      android:textStyle="bold" 
      android:textColor="@color/link_color" 
      android:visibility="gone" 
      style="?attr/buttonBarButtonStyle"/> 

     <Button 
      android:id="@+id/positive_button" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@android:string/ok" 
      android:textStyle="bold" 
      android:textColor="@color/link_color" 
      android:visibility="gone" 
      style="?attr/buttonBarButtonStyle"/> 

    </LinearLayout> 

</LinearLayout> 

+0

無法重現此操作。你能否添加你的代碼? – reVerse

+0

@reVerse,我剛剛加了我的代碼 –

回答

18

這是AppCompatDialogFragment的標題。您可以用此代碼隱藏標題:

public class AlertDialogFragment extends AppCompatDialogFragment { 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setStyle(DialogFragment.STYLE_NO_TITLE, 0); 
    } 
} 
+2

太棒了!它現在的作品! –

+1

花一個小時尋找一個可行的修復程序,謝謝,從字面上看,沒有其他的工作... – Benjiko99

+1

記得把它放在onCreate中,它不會在創建視圖後工作。這個答案節省了我很多時間,謝謝。 – Youtoo