因此,对于练习,我正在学习如何以编程方式创建视图。我创建了一个扩展Viewgroup的新布局(我称之为Custom1),它将子视图(所有大小相同)放在两列中。
这个组的孩子也是一个自定义布局(我称之为Custom2),其中包含一个imageview和两个textview。我使用for循环向视图组添加必要数量的视图,并覆盖onLayout。
现在,我试图在Nexus 4上使用" show layout bounds"选项已选中。我可以看到Custom1的子项的边界都在正确的位置,并且基于日志,custom2的子项也在正确的位置。但是,只有第一个" custom2"正确显示(即第一个custom2显示一个imageView和两个textview,其余为空)。
父视图是否可能掩盖了子视图?
如果没有,有没有人遇到过类似的问题?
以下是我的一些custom1代码:
@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
截图。