Android屏幕底部彈出DialogFragment(3)




Android屏幕底部彈出DialogFragment(3)

附錄文章1,2的DialogFragment是常規的DialogFragment,但是現在的一些Android開發中,往往需要從底部彈出一個功能對話框供用戶選擇使用。這種底部彈出的對話框開源項目也很多,比如附錄文章3,4,5,6,7,8,9,10,11。
對Android原生的DialogFragment進行改造,也可以實現底部彈出的對話框(面板)。重點是要設置重載DialogFragment的Gravity位置:Gravity.BOTTOM。
寫一個例子:

package zhangphil.demo;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.app.DialogFragment;
import android.app.FragmentManager;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		final FragmentManager fm = this.getFragmentManager();

		findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				BottomDialogFragment f = new BottomDialogFragment();
				f.show(fm, BottomDialogFragment.class.getName());
			}
		});
		;
	}

	public class BottomDialogFragment extends DialogFragment {

		@Override
		public Dialog onCreateDialog(Bundle savedInstanceState) {

			Dialog dialog = new Dialog(getActivity(), R.style.BottomFragmentDialog);

			// 必須在setContentView之前調用。否則運行時報錯。
			dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

			LayoutInflater inflater = getActivity().getLayoutInflater();
			View view = inflater.inflate(android.R.layout.simple_list_item_2, null);
			TextView text1 = (TextView) view.findViewById(android.R.id.text1);
			text1.setText("zhang phil");
			TextView text2 = (TextView) view.findViewById(android.R.id.text2);
			text2.setText("zhang phil @ csdn");

			// 底部彈出的DialogFragment裝載的View
			dialog.setContentView(view);

			dialog.setCancelable(true);
			dialog.setCanceledOnTouchOutside(true);

			// 設置底部彈出顯示的DialogFragment窗口屬性。
			Window window = dialog.getWindow();
			WindowManager.LayoutParams params = window.getAttributes();
			params.gravity = Gravity.BOTTOM;
			params.width = WindowManager.LayoutParams.MATCH_PARENT;
			params.height = 1000; // 底部彈出的DialogFragment的高度,如果是MATCH_PARENT則鋪滿整個窗口
			window.setAttributes(params);

			return dialog;
		}
	}
}


需要在styles.xml文件里面定義一個style:

 <style name="BottomFragmentDialog" parent="@style/AppTheme">
        <item name="android:layout_width">match_parent</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:windowIsFloating">true</item>
    </style>



代碼運行結果:




附錄:
1,《Android DialogFragment(1)》鏈接地址:http://blog.csdn.net/zhangphil/article/details/50886077
2,《Android DialogFragment(2)》鏈接地址:http://blog.csdn.net/zhangphil/article/details/50923828

Android BottomSheet相關:
3,《Android BottomSheet:便捷易用的底部滑出面板(1)》鏈接地址:http://blog.csdn.net/zhangphil/article/details/51775955 
4,《Android BottomSheet:以選取圖片為例(2)》鏈接地址:http://blog.csdn.net/zhangphil/article/details/51776408 
5,《Android BottomSheet:List列表或Grid網格展示(3)》鏈接地址:http://blog.csdn.net/zhangphil/article/details/51781698
6,《Android BottomSheet:底部彈出Fragment面板(4)》鏈接地址:http://blog.csdn.net/zhangphil/article/details/51787875

AndroidSweetSheet相關:
7,《AndroidSweetSheet:從底部彈出面板(1)》鏈接地址:http://blog.csdn.net/zhangphil/article/details/51790845
8,《AndroidSweetSheet:ViewPager的實現(2)》鏈接地址:http://blog.csdn.net/zhangphil/article/details/51791210


AndroidSlidingUpPanel相關:
9,《Android自底部平滑向上滑出面板的AndroidSlidingUpPanel》鏈接地址:http://blog.csdn.net/zhangphil/article/details/51444509  

Android DraggablePanel相關:
10,《Android音樂、視頻類APP常用控件:DraggablePanel(1)》鏈接地址:http://blog.csdn.net/zhangphil/article/details/51566860  
11,《Android音樂、視頻類APP常用控件:DraggablePanel(2)》鏈接地址:http://blog.csdn.net/zhangphil/article/details/51578665


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM