当前位置:网站首页>设置自定义dialog的正确宽高
设置自定义dialog的正确宽高
2022-06-13 05:22:00 【-SOLO-】
对于自定义dialog。要获取其宽高最好的办法是通过DecorView,因为Activity的DecorView的宽高就是屏幕的宽高。而通过DecorView来测试宽高。会考虑到layout_width和layout_height。这个两个属性的影响。
如果直接通过
view.measure(0,0);
这种方法。测量的大小是其子控件的大小。没办法考虑到屏幕的大小。相对比较麻烦。
因此推荐以下这种。
记住inflate的第三个参数为false。因为我们只需layoutParams不需要真的添加到DecorView上去。
View view = inflater.inflate(R.layout.layout_dialog, (ViewGroup) getActivity().getWindow().getDecorView(), false);
//获取大小
measuredWidth = view.getLayoutParams().width;
measuredHeight = view.getLayoutParams().height;
以下是途径的一种方式。设置dialog的大小必须通过设置其window的Attributes来实现。而最好的时机就是onViewCreate
为了去除干扰必须设置以下
padding的设置必须在backgroud之后
getDialog().getWindow().getDecorView().setBackground(new ColorDrawable(Color.BLUE));
getDialog().getWindow().getDecorView().setPadding(0,0,0,0);
public class MyFragment extends DialogFragment {
private int measuredWidth;
private int measuredHeight;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.layout_dialog, (ViewGroup) getActivity().getWindow().getDecorView(), false);
measuredWidth = view.getLayoutParams().width;
measuredHeight = view.getLayoutParams().height;
return view;
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
if (getDialog() != null) {
WindowManager.LayoutParams attributes = getDialog().getWindow().getAttributes();
attributes.width = measuredWidth;
attributes.height = measuredHeight;
attributes.windowAnimations=R.style.dialogWindowAnim;
attributes.gravity= Gravity.TOP|Gravity.START;
getDialog().getWindow().setAttributes(attributes);
getDialog().getWindow().getDecorView().setPadding(0,0,0,0);
getDialog().getWindow().getDecorView().setBackground(new ColorDrawable(Color.RED));
}
}
}
边栏推荐
- RT thread console device initialization
- Case -- the HashSet set stores the student object and traverses
- MySQL built-in functions
- Difference between deviation and variance in deep learning
- Simple-SR:Best-Buddy GANs for Highly Detailed Image Super-Resolution论文浅析
- First assessment
- C language learning log 10.5
- Clause 48: understand template metaprogramming
- Recursion and recursion
- COAP protocol libcoap API
猜你喜欢

KVM hot migration for KVM virtual management

Wampserver (MySQL) installation

Time display of the 12th Blue Bridge Cup

Case - the ArrayList collection stores student objects and traverses them in three ways

Mysql database crud operation

MySQL log management and master-slave replication

安装harbor(在线|离线)

Chapter 18 pagination: Introduction

890. Find and Replace Pattern

Case - the list set stores student objects and traverses them in three ways
随机推荐
Windbos run command set
Mysql database backup and restore:
Case -- the HashSet set stores the student object and traverses
File descriptorfile description
Pychart error resolution: process finished with exit code -1073741819 (0xc0000005)
Enhanced for loop
Case - random numbers without repetition (HashSet and TreeSet)
语音信号分帧的理解
Fast power code
C language learning log 1.17
Sort (internal sort) + external sort
Nonstandard compiler attribute extension
Browser screenshot method (long screenshot, node screenshot, designated area screenshot)
Simple-SR:Best-Buddy GANs for Highly Detailed Image Super-Resolution論文淺析
ZABBIX proxy, sender (without agent monitoring), performance optimization
MySQL advanced query
C language learning log 10.4
Simple sr: Best Buddy Gans for highly detailed image super resolution Paper Analysis
Binary search and binary answer
Case - count the number of occurrences of each string in the string