make.bottom.equalTo(lastView.mas_bottom).offset(10);只是contentView的下边缘在最后一个view的下边10,不能保证contentview大于等于scrollvew
时间: 2024-04-22 22:23:34 浏览: 25
这是一个使用 Masonry 布局库的代码,它的作用是将 contentView 的底边缘与最后一个 view 的底边缘相差 10 个点的距离。但是这段代码不能保证 contentView 大于或等于 scrollView。如果 contentView 的高度不够大,那么最后一个 view 将会超出 contentView 的底部,导致无法滚动到最后一个 view。因此,在使用这段代码时需要确保 contentView 的高度足够大,能够容纳所有的子视图。
相关问题
在UIScrollview中添加子view,在子view小于scrollview,如何确保contentView的高度大于scrollview,使用Masory
可以使用Masonry来实现在UIScrollView中添加子视图,并确保contentView的高度大于UIScrollView的高度。
具体实现步骤如下:
1.创建UIScrollView和其子视图,并将子视图添加到UIScrollView中:
UIScrollView *scrollView = [[UIScrollView alloc] init];
[self.view addSubview:scrollView];
UIView *contentView = [[UIView alloc] init];
[scrollView addSubview:contentView];
2.使用Masonry设置UIScrollView和其子视图的约束:
[scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self.view);
}];
[contentView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(scrollView);
make.width.equalTo(scrollView);
}];
3.在子视图添加完后,使用Masonry计算contentView的高度:
[contentView mas_makeConstraints:^(MASConstraintMaker *make) {
make.bottom.equalTo(lastView.mas_bottom).offset(10);
}];
其中lastView为子视图中的最后一个视图,10为最后一个视图与contentView之间的距离。
通过以上步骤,可以在UIScrollView中添加子视图,并确保contentView的高度大于UIScrollView的高度。
Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?
这个错误通常是由于在Activity的生命周期函数还没有执行完毕时,就尝试在PopupWindow或Dialog中添加窗口导致的。具体来说,可能是在Activity的onCreate函数中尝试添加PopupWindow或Dialog,而此时Activity的生命周期函数还没有执行完毕,所以无法获取到正确的Window Token。解决方法是将PopupWindow或Dialog的添加操作放到Activity的onWindowFocusChanged函数中,这个函数会在Activity的生命周期函数全部执行完毕之后被调用,此时就可以获取到正确的Window Token了。
以下是一个示例代码:
```java
public class MainActivity extends AppCompatActivity {
private Button mButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mButton = findViewById(R.id.button);
}
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if (hasFocus) {
// 在这里添加PopupWindow或Dialog
PopupWindow popupWindow = new PopupWindow(this);
View contentView = LayoutInflater.from(this).inflate(R.layout.popup_window, null);
popupWindow.setContentView(contentView);
popupWindow.showAsDropDown(mButton);
}
}
}
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)