视图组错误
ViewGroup Errors
因此,为了练习,我正在学习如何以编程方式创建视图。我创建了一个扩展 Viewgroup(我称之为 Custom1)的新布局,它将子视图(所有相同大小)放在两列中。
该组的子项也是包含一个图像视图和两个文本视图的自定义布局(我称之为 Custom2)。我使用 for 循环将必要数量的视图添加到视图组,并且覆盖了 onLayout。
现在,我尝试在选中 "show layout bounds" 选项的 Nexus 4 上 运行 此操作。我可以看到 Custom1 的子项的边界都在正确的位置,并且根据日志,custom2 的子项也在正确的位置。但是,只有第一个 "custom2" 正确显示(即第一个 custom2 显示一个 imageView 和两个 textview,其余为空)。
有没有可能是父视图覆盖了子视图?
如果没有,有没有人运行以前遇到过类似的问题?
这是我的一些自定义 1 代码:
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
final int count = getChildCount();
if (ItemToDebug.equals("Layout")){
if (count == numberOfChannels){
Log.d("Layout:", "Number of children matches number of channels");
}else{
Log.d("Layout:", "Mismatch between number of children and number of channels");
}
Log.d("Layout:", "onLayout " + Integer.toString(count) + " children");
}
for (int i = 0; i < count; i++){
View child = getChildAt(i);
if (i%2 == 0){
if (LayoutLeftChild(i/2, l, t, r, b, child)){ //Lays out Child, returning a Boolean if successful
if (ItemToDebug.equals("Layout")){
Log.d("Layout:", "onLayoutLeftChild number " + Integer.toString(i/2) + "successful");
}
} else
if (ItemToDebug.equals("Layout")){
Log.d("Layout:", "onLayoutLeftChild number " + Integer.toString(i/2) + "failed");
}
}
if (i%2 == 1){
if (LayoutRightChild(i/2, l, t, r, b, child)){
if (ItemToDebug.equals("Layout")){
Log.d("Layout:", "onLayoutRightChild number " + Integer.toString(i/2) + "successful");
}
}else{
if (ItemToDebug.equals("Layout")){
Log.d("Layout:", "onLayoutRightChild number " + Integer.toString(i/2) + "failed");
}
}
}
}
}
/*
Left edge for right column = l + (r-l)/2 + 15
Right edge for right column = r - 20
20dp Margin between rows
Rows are 400 dp tall
*/
private boolean LayoutRightChild(int i, int l, int t, int r, int b, View child) {
final View Child = child;
final int Left = l + (r-l)/2 + 15;
final int Right = r - 20;
final int Top = t + i*20 + (i-1)*400;
final int Bottom = Top + 400;
Child.layout(Left, Top, Right, Bottom);
if (ItemToDebug.equals("Layout")){
Log.d("Layout:", "Child laid out at (" + Integer.toString(Left) + ", " + Integer.toString(Top) + ", " + Integer.toString(Right) + ", " + Integer.toString(Bottom) + ")");
}
return true;
}
/*
Left edge for left column = l + 20
Right edge for left column = l + (r-l)/2 - 15
20dp Margin between rows
Rows are 400 dp tall
*/
private boolean LayoutLeftChild(int i, int l, int t, int r, int b, View child) {
final View Child = child;
final int Left = l + 20;
final int Right = l + (r-l)/2 - 15;
final int Top = t + i*20 + (i-1)*400;
final int Bottom = Top + 400;
Child.layout(Left, Top, Right, Bottom);
if (ItemToDebug.equals("Layout")){
Log.d("Layout:", "Child laid out at (" + Integer.toString(Left) + ", " + Integer.toString(Top) + ", " + Integer.toString(Right) + ", " + Integer.toString(Bottom) + ")");
}
return true;
}
这是来自 custom2 的一些代码:
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
final int count = getChildCount(); //There should be three children - one ImageView on top and two TextViews on bottom
if (ItemToDebug.equals("Layout")){
if (count == 3){
Log.d("View Contents:", "3 Children Views found");
}else{
Log.d("View Contents:", "Number of Children Incorrect. " + Integer.toString(count) + " children found.");
}
Log.d("Layout:", "onLayout " + Integer.toString(count) + " children");
}
//Get children here in for loop and place.
for (int i = 0; i < count; i++){
final View child = this.getChildAt(i);
//Layout should already have margins, so align contents with left and right sides.
int width = r - l;
int top;
int height;
switch(i){
case 0:
top = t;
height = 100;
if (ItemToDebug.equals("Layout")) {
Log.d("Layout:", "Image Laid out at (" + Integer.toString(l) + ", " + Integer.toString(top) + ", " + Integer.toString(r) + ", " + Integer.toString(top + height) + ")");
}
break;
case 1:
top = t + 100;
height = 60;
if (ItemToDebug.equals("Layout")) {
Log.d("Layout:", "TextView (nowPlaying) Laid out at (" + Integer.toString(l) + ", " + Integer.toString(top) + ", " + Integer.toString(r) + ", " + Integer.toString(top + height) + ")");
}
break;
case 2:
top = t + 160;
height = 60;
if (ItemToDebug.equals("Layout")) {
Log.d("Layout:", "TextView (nextPlaying) Laid Out at (" + Integer.toString(l) + ", " + Integer.toString(top) + ", " + Integer.toString(r) + ", " + Integer.toString(top + height) + ")");
}
break;
default:
top = t;
height = 0;
if (ItemToDebug.equals("Layout")){
Log.d("Layout:", "More than 3 children have been added to the Custom2");
}
break;
}
child.layout(l, top, r, top + height);
}
}
这是我的日志:
https://drive.google.com/file/d/0B0EGM_1_9jazWEZ1RXh4a2VXdnM/view?usp=sharing
以及截图。
可以在这里找到答案:Views within a Custom ViewGroup are not showing
Children 是我们的 parents(左边和上面是 0)
因此,为了练习,我正在学习如何以编程方式创建视图。我创建了一个扩展 Viewgroup(我称之为 Custom1)的新布局,它将子视图(所有相同大小)放在两列中。
该组的子项也是包含一个图像视图和两个文本视图的自定义布局(我称之为 Custom2)。我使用 for 循环将必要数量的视图添加到视图组,并且覆盖了 onLayout。
现在,我尝试在选中 "show layout bounds" 选项的 Nexus 4 上 运行 此操作。我可以看到 Custom1 的子项的边界都在正确的位置,并且根据日志,custom2 的子项也在正确的位置。但是,只有第一个 "custom2" 正确显示(即第一个 custom2 显示一个 imageView 和两个 textview,其余为空)。
有没有可能是父视图覆盖了子视图?
如果没有,有没有人运行以前遇到过类似的问题?
这是我的一些自定义 1 代码:
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
final int count = getChildCount();
if (ItemToDebug.equals("Layout")){
if (count == numberOfChannels){
Log.d("Layout:", "Number of children matches number of channels");
}else{
Log.d("Layout:", "Mismatch between number of children and number of channels");
}
Log.d("Layout:", "onLayout " + Integer.toString(count) + " children");
}
for (int i = 0; i < count; i++){
View child = getChildAt(i);
if (i%2 == 0){
if (LayoutLeftChild(i/2, l, t, r, b, child)){ //Lays out Child, returning a Boolean if successful
if (ItemToDebug.equals("Layout")){
Log.d("Layout:", "onLayoutLeftChild number " + Integer.toString(i/2) + "successful");
}
} else
if (ItemToDebug.equals("Layout")){
Log.d("Layout:", "onLayoutLeftChild number " + Integer.toString(i/2) + "failed");
}
}
if (i%2 == 1){
if (LayoutRightChild(i/2, l, t, r, b, child)){
if (ItemToDebug.equals("Layout")){
Log.d("Layout:", "onLayoutRightChild number " + Integer.toString(i/2) + "successful");
}
}else{
if (ItemToDebug.equals("Layout")){
Log.d("Layout:", "onLayoutRightChild number " + Integer.toString(i/2) + "failed");
}
}
}
}
}
/*
Left edge for right column = l + (r-l)/2 + 15
Right edge for right column = r - 20
20dp Margin between rows
Rows are 400 dp tall
*/
private boolean LayoutRightChild(int i, int l, int t, int r, int b, View child) {
final View Child = child;
final int Left = l + (r-l)/2 + 15;
final int Right = r - 20;
final int Top = t + i*20 + (i-1)*400;
final int Bottom = Top + 400;
Child.layout(Left, Top, Right, Bottom);
if (ItemToDebug.equals("Layout")){
Log.d("Layout:", "Child laid out at (" + Integer.toString(Left) + ", " + Integer.toString(Top) + ", " + Integer.toString(Right) + ", " + Integer.toString(Bottom) + ")");
}
return true;
}
/*
Left edge for left column = l + 20
Right edge for left column = l + (r-l)/2 - 15
20dp Margin between rows
Rows are 400 dp tall
*/
private boolean LayoutLeftChild(int i, int l, int t, int r, int b, View child) {
final View Child = child;
final int Left = l + 20;
final int Right = l + (r-l)/2 - 15;
final int Top = t + i*20 + (i-1)*400;
final int Bottom = Top + 400;
Child.layout(Left, Top, Right, Bottom);
if (ItemToDebug.equals("Layout")){
Log.d("Layout:", "Child laid out at (" + Integer.toString(Left) + ", " + Integer.toString(Top) + ", " + Integer.toString(Right) + ", " + Integer.toString(Bottom) + ")");
}
return true;
}
这是来自 custom2 的一些代码:
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
final int count = getChildCount(); //There should be three children - one ImageView on top and two TextViews on bottom
if (ItemToDebug.equals("Layout")){
if (count == 3){
Log.d("View Contents:", "3 Children Views found");
}else{
Log.d("View Contents:", "Number of Children Incorrect. " + Integer.toString(count) + " children found.");
}
Log.d("Layout:", "onLayout " + Integer.toString(count) + " children");
}
//Get children here in for loop and place.
for (int i = 0; i < count; i++){
final View child = this.getChildAt(i);
//Layout should already have margins, so align contents with left and right sides.
int width = r - l;
int top;
int height;
switch(i){
case 0:
top = t;
height = 100;
if (ItemToDebug.equals("Layout")) {
Log.d("Layout:", "Image Laid out at (" + Integer.toString(l) + ", " + Integer.toString(top) + ", " + Integer.toString(r) + ", " + Integer.toString(top + height) + ")");
}
break;
case 1:
top = t + 100;
height = 60;
if (ItemToDebug.equals("Layout")) {
Log.d("Layout:", "TextView (nowPlaying) Laid out at (" + Integer.toString(l) + ", " + Integer.toString(top) + ", " + Integer.toString(r) + ", " + Integer.toString(top + height) + ")");
}
break;
case 2:
top = t + 160;
height = 60;
if (ItemToDebug.equals("Layout")) {
Log.d("Layout:", "TextView (nextPlaying) Laid Out at (" + Integer.toString(l) + ", " + Integer.toString(top) + ", " + Integer.toString(r) + ", " + Integer.toString(top + height) + ")");
}
break;
default:
top = t;
height = 0;
if (ItemToDebug.equals("Layout")){
Log.d("Layout:", "More than 3 children have been added to the Custom2");
}
break;
}
child.layout(l, top, r, top + height);
}
}
这是我的日志:
https://drive.google.com/file/d/0B0EGM_1_9jazWEZ1RXh4a2VXdnM/view?usp=sharing
以及截图。
可以在这里找到答案:Views within a Custom ViewGroup are not showing
Children 是我们的 parents(左边和上面是 0)