【问题标题】:Set the child view at the TopLeft and BottomRight in a viewgroup在视图组中的 TopLeft 和 BottomRight 设置子视图
【发布时间】:2013-05-06 01:11:31
【问题描述】:

在我的应用程序中,我的布局如下:

1) 主视图可以移动和拖动。

2) 一些控制器可以添加到主视图中。他们的位置可以设置为TopLeft 或其他。这里的TopLeft 表示相对于主视图。(见this image

现在我尝试通过扩展ViewGroup 来实现这一点,而main Viewcontrollers 都是从View 扩展而来的。

这就是我现在所做的:

class MyViewContainer extends ViewGroups{
   @Override
   public void onLayout(boolean changed,int l,int t,int b, int r){

      for(int i=0,len=getChildCounts,i<len;i++){
        View v=getChildAt(i);
        if(v instanceof MainView){
           v.layout(l,t,b,r);
        }
        else if(v instanceof MyController){
          //here do not now know how to layout the childs according to their position
        }
      }
   }
}
abstract MyController extends View{
}
class ZoomController extends MyController{
    @Override
    public void onDraw(Canvas c){

    }
}
class AnotherController extends MyController{
    @Override
    public void onDraw(Canvas c){

    }
}
class MainView extends View{
    @Override
    public void onDraw(Canvas c){

    }
}

如您所见,我不知道如何布局MyControllers。因为我不完全知道controller 的宽度和高度。因为控制器的大小取决于它的绘制方式。

如何制作?

【问题讨论】:

    标签: android layout view viewgroup


    【解决方案1】:

    试试这个。为此,主视图必须位于控制器之前。

    class MyViewContainer extends ViewGroups{
        @Override
        public void onLayout(boolean changed,int l,int t,int b, int r){
            View masterView = null;
            for(int i=0,len=getChildCounts,i<len;i++){
                View v=getChildAt(i);
                if(v instanceof MainView){
                    masterView = v;
                    v.layout(l,t,b,r);
                }
                else if(v instanceof MyController){
                    // Layout your views here.
                    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
                    if (masterView != null && topLeft) {
                        params.addRule(RelativeLayout.ALIGN_LEFT, masterView.getId());
                        params.addRule(RelativeLayout.ALIGN_TOP, masterView.getId());
                    }
                    if (masterView != null && bottomRight) {
                        params.addRule(RelativeLayout.ALIGN_RIGHT, masterView.getId());
                        params.addRule(RelativeLayout.ALIGN_BOTTOM, masterView.getId());
                    }
                    v.setLayoutParams(params);
                }
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-01-27
      • 2020-04-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多