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