问题描述
在这里亏损。搜索广泛,找不到这个问题的答案...我有一个XML布局,我有一个相对的布局,我用作一个地方持有人的不同意见。
< RelativeLayout
android:id =@ + id / relVariableLayoutPlaceHolder
android:layout_width =match_parent
android:layout_height =0dp
android:layout_weight =1
android:background =@ color /白色
>
< / RelativeLayout>
在我的活动中,我有一个按钮供用户按,当他们按它,我想要创建并动态添加的定制 ClockView
请注意, _variableLayout
在 onCreate
方法中完美设置:
public void onStartTimeClick(View view)
{
//清理任何剩余的视图
_variableLayout.removeAllViews();
//添加时钟视图
ClockView clockView = new ClockView(_context);
_variableLayout.addView(clockView);
}
问题是当按下按钮时,屏幕上没有任何反应。我可以看到它,当我点击一个 TextView
也是在屏幕上。
我上传了一些图像之前和之后点击 TextView
:
我可以想象,这与在时钟视图绘制后不被刷新的屏幕有关但是我已经尝试过 invalidate()
和 requestLayout()
的所有组合。任何想法?
为了历史的目的,我觉得我应该提到,经过很多调试,我发现我的问题在 onMeasure
和 onLayout
功能的 ClockView
中。基本上,他们的形式不好,没有正确地评估 onLayout
函数的子视图正确处理。
一旦我得到了这些消息,一切都按预期工作。
At a loss here. Searched far and wide and can't find an answer to this question...
I have an XML layout where I have a relative layout that I am using as a place holder for different views.
<RelativeLayout
android:id="@+id/relVariableLayoutPlaceHolder"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="@color/white"
>
</RelativeLayout>
In my activity, I have a button for the user to press and when they press it, I want the custom ClockView
to be created and dynamically added. Note that the _variableLayout
is setting perfectly in the onCreate
method:
public void onStartTimeClick(View view)
{
//Clean up any left over views
_variableLayout.removeAllViews();
//Add clock view
ClockView clockView = new ClockView(_context);
_variableLayout.addView(clockView);
}
Problem is that when the button is pressed, nothing happens on the screen. I can, however see it when I tap a TextView
that is also on the screen.
I uploaded some images here of the before and after clicking the TextView
:http://imgur.com/a/9lebv
I would imagine this has something to do with the screen not being refreshed after the clock view is drawn but I have tried every combination of invalidate()
and requestLayout()
I can think of. Any ideas?
For historical purposes, I felt that I should mention that after a lot of debugging I figured out that my problem was in the onMeasure
and onLayout
functions of the ClockView
. Basically they were poorly formed and not measuing the subviews properly for the onLayout
function to handle properly.
Once I got those ironed out, everything was working as expected.
这篇关于自定义视图不显示动态添加的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!