- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我想在绘制后获取布局的宽度
和高度
。
我的代码实际上是在createView()
方法中调用的。但我想等到布局绘制完成后再执行这段代码:
Log.i(TAG, "Height: "+myButton.getHeight()+" - width : "+myButton.getWidth());
// result is Height: 0 - width : 0
在 onCreateView()
调用后是否启动了任何事件?
最佳答案
使用全局布局监听器对我来说一直很有效。它的优点是能够在布局发生变化时重新测量事物,例如如果某些内容设置为 View.GONE 或添加/删除了 subview 。
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// inflate your main layout here (use RelativeLayout or whatever your root ViewGroup type is
LinearLayout mainLayout = (LinearLayout ) this.getLayoutInflater().inflate(R.layout.main, null);
// set a global layout listener which will be called when the layout pass is completed and the view is drawn
mainLayout.getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener() {
public void onGlobalLayout() {
//Remove the listener before proceeding
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
mainLayout.getViewTreeObserver().removeOnGlobalLayoutListener(this);
} else {
mainLayout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
// measure your views here
}
}
);
setContentView(mainLayout);
根据您的使用情况,您可能希望在使用完监听器后将其移除,如图所示。
关于Android - createView 后的事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23128305/
我正在学习 Django,我在从 CreateView 重定向回来时遇到问题 我想重定向到 BookDetail 页面,其中包含由 CreateView 创建的 bookinstances 列表。模型
假设我们有一个名为 Closet 的应用程序,它有一些模型: # closet.models.py class Outfit(models.Model): shirt = models.
我如何在 Django 的 Createview 中声明一个变量,以便我可以从它的模板中使用它? 例如我想在模板中使用 {{ place_slug }} 。我从 urls.py 传递它,如下所示: 网
我正在尝试在我的 Django 1.8 应用程序中实现通用 View ,以便 Django 可以为我处理验证/重定向循环。 我已经创建了一个模型: class Customer(models.Mode
我有一个这样的模型: class Appointment(models.Model): user = models.ForeignKey(User) engineer = models
我正在开发一个包含章节的项目,每个章节都有标题、内容和顺序。我想保留字段“顺序”的命名,但将该字段显示在 CreateView 中作为其他内容,例如“章节号”。我发现的最佳信息建议更新 Meta 类中
型号: class MyModel(model.Models) status = models.ForeignKey(Statuses, on_delete=models.PROTECT, nu
我创建了一个Form ,其中用户设置 Alarm目的。 Alarm对象按预期保存到数据库中。然而问题是:另一个对象,它只包含 Form 中填写的信息。 ,也保存到数据库。 据我了解,form_vali
我有两个串联的表格。基本上用户填写第一个表单,然后被重定向到第二个表单,第二个表单为第一个表单的数据增加值(value)。例如。我有一个表单电影(第一个表单),然后我被重定向到将 Actor 添加到电
我想在绘制后获取布局的宽度和高度。 我的代码实际上是在createView()方法中调用的。但我想等到布局绘制完成后再执行这段代码: Log.i(TAG, "Height: "+myButton.ge
我有 2 个模型,Father 和 Son。 我有一个注册父亲的页面。在同一页面上,我有一个用于注册 Son 的表单集。 页面上有一个“更多”按钮,用于在同一页面上添加另一个Father 和他们各自的
使用 CreateView 类,我想保存多个数据条目。 输入示例: 项目是“苹果、香蕉、胡萝卜” 位置是“位置 1” 我想像这样将它们保存到数据库中: [苹果,位置 1] [香蕉,位置 1] [胡萝卜
我有这个模型,我需要的是唯一的时间,因为它是一个预订,所以当我创建一个新的预订时,我如何检查那个时间是否已经被选择。 模型.py class Reserva(models.Model): ho
在这里,我使用 CreateView 创建项目,然后我重定向用户以更新当前创建的对象的其他字段。 这是我的代码: Views.py class DynamicCreate(CreateView):
这是我第一次真正使用 Django (1.6),我一直无法弄清楚: 我试图将 CreateView 上的下拉列表限制为仅显示在项目模型中具有事件状态 (2) 的项目: class ProjectSta
我正在尝试实现一个预约应用程序,用户可以在其中创建与预先存在的类相关联的 session 。我想要做的是使用 django CreateView 创建一个 session ,而不要求用户提供关联的类,
Django FormView 和 CreateView 有什么区别? 我看到的唯一区别是,FormView 需要 ModelForm 但 CreateView 不需要。 否则,两者都会做同样的事情来
我正在尝试采用一种使用 Django-Crispy-Forms 布局功能保存带有主表单的嵌套表单集的方法,但我无法保存它。我正在关注this代码示例项目,但无法验证表单集以保存数据。如果有人能指出我的
我正在使用自定义的CreateView(CourseCreate)和UpdateView(CourseUpdate)保存和更新类(class)。保存类(class)后,我想采取措施。我将在新类(cla
在我看来.py: class DatasetRequestCreateView(CreateView): model = DatasetRequest form_class = Dat
我是一名优秀的程序员,十分优秀!