0

我正在尝试使用RelativeLayout 来生成两行项目,一排三个按钮在上面,下面有一个滚动视图。我希望 scrollView 尽可能多地占据空间。

我相信 RelativeLayout 是这里最好的方法。我试图在运行时而不是通过 xml 布局文件来执行此操作。我的代码如下:

            RelativeLayout.LayoutParams exitparams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,  
                    RelativeLayout.LayoutParams.WRAP_CONTENT); 


            exitparams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);  

            RelativeLayout.LayoutParams zimparams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,  
                    RelativeLayout.LayoutParams.WRAP_CONTENT); 
            RelativeLayout.LayoutParams zoutparams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,  
                    RelativeLayout.LayoutParams.WRAP_CONTENT); 
            RelativeLayout.LayoutParams scrollparams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,  
                    RelativeLayout.LayoutParams.WRAP_CONTENT);

            zimparams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); 
            scrollparams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM): 



            // Exit button
            Button exitbutton = new Button(this);
            exitbutton.setText("Exit");
            exitbutton.setGravity(Gravity.CENTER);
            exitbutton.setWidth(200);
            exitbutton.setLayoutParams(exitparams);


            // Zoom In button
            Button zoomOutButton = new Button(this);
            zoomOutButton.setText("Zoom Out");
            zoomOutButton.setGravity(Gravity.CENTER);
            zoomOutButton.setWidth(200);
            zoomOutButton.setLayoutParams(zimparams);


            // Zoom In button
            Button zoomInButton = new Button(this);
            zoomInButton.setText("Zoom In");
            zoomInButton.setGravity(Gravity.CENTER);
            zoomInButton.setWidth(200);
            zoutparams.addRule(RelativeLayout.LEFT_OF, zoomInButton.getId());  

            zoomInButton.setLayoutParams(zoutparams);

            RelativeLayout layout1 = new RelativeLayout(this);  
            layout1.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.FILL_PARENT));  
            layout1.addView(exitbutton);  
            layout1.addView(zoomInButton);  
            layout1.addView(zoomOutButton);  





            exitbutton.setOnClickListener (new View.OnClickListener() {
                public void onClick(View v) {
                   finish();
                }
            });

            zoomInButton.setOnClickListener (new View.OnClickListener() {
                public void onClick(View v) {
                    increaseZoom();
                    onResume();
                   }
            });

            zoomOutButton.setOnClickListener (new View.OnClickListener() {
                public void onClick(View v) {
                   reduceZoom();
                   onResume();
                   }
            });

            drawView = new DrawView(this, height, width, SMD, zoomLevel);
            drawView.setBackgroundColor(Color.WHITE);
            ScrollView scrollView = new ScrollView(this);
            scrollView.addView(drawView);
            scrollView.setLayoutParams(scrollparams);

            layout1.addView(scrollView);
            setContentView(layout1);

然而,唯一出现的是滚动视图。我猜我在这里遗漏了一些小东西......我阅读了文档,我相信我这样做是正确的,但它不起作用的事实表明我要么做错了什么,要么无法完成我想做的事情。

我希望它看起来如何的视觉效果如下:

布局

4

1 回答 1

1

您需要setContentView(layout1)在最后调用以实际添加布局。

编辑:

好的,我已经修复了你的代码,它在我的手机上工作。但是我显然无法合作,DrawView所以如果有什么问题,我无法帮助你。试一试。

     RelativeLayout.LayoutParams exitparams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,  
             RelativeLayout.LayoutParams.WRAP_CONTENT); 


     exitparams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);  

     RelativeLayout.LayoutParams zimparams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,  
             RelativeLayout.LayoutParams.WRAP_CONTENT); 
     RelativeLayout.LayoutParams zoutparams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,  
             RelativeLayout.LayoutParams.WRAP_CONTENT); 
     RelativeLayout.LayoutParams scrollparams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,  
             RelativeLayout.LayoutParams.WRAP_CONTENT);

     zimparams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); 




     // Exit button
     Button exitbutton = new Button(this);
     exitbutton.setText("Exit");
     exitbutton.setGravity(Gravity.CENTER);
     exitbutton.setWidth(200);
     exitbutton.setLayoutParams(exitparams);
     exitbutton.setId(2);


     // Zoom In button
     Button zoomOutButton = new Button(this);
     zoomOutButton.setText("Zoom Out");
     zoomOutButton.setGravity(Gravity.CENTER);
     zoomOutButton.setWidth(200);
     zoomOutButton.setLayoutParams(zimparams);
     zoomOutButton.setId(1);

     // Zoom In button
     Button zoomInButton = new Button(this);
     zoomInButton.setText("Zoom In");
     zoomInButton.setGravity(Gravity.CENTER);
     zoomInButton.setWidth(200);
     zoutparams.addRule(RelativeLayout.LEFT_OF, 1);  
     zoutparams.addRule(RelativeLayout.RIGHT_OF, 2);  

     zoomInButton.setLayoutParams(zoutparams);

     RelativeLayout layout1 = new RelativeLayout(this);  
     layout1.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.FILL_PARENT));  
     layout1.addView(exitbutton);  
     layout1.addView(zoomInButton);  
     layout1.addView(zoomOutButton);  





     exitbutton.setOnClickListener (new View.OnClickListener() {
         public void onClick(View v) {
            finish();
         }
     });

     zoomInButton.setOnClickListener (new View.OnClickListener() {
         public void onClick(View v) {
             //increaseZoom();
             onResume();
            }
     });

     zoomOutButton.setOnClickListener (new View.OnClickListener() {
         public void onClick(View v) {
            //reduceZoom();
            onResume();
            }
     });

     //drawView = new DrawView(this, height, width, SMD, zoomLevel);
    // drawView.setBackgroundColor(Color.WHITE);
     ScrollView scrollView = new ScrollView(this);
     //scrollView.addView(drawView);
     scrollparams.addRule(RelativeLayout.BELOW,1);
     scrollView.setLayoutParams(scrollparams);

     layout1.addView(scrollView);
     setContentView(layout1);
于 2013-08-08T03:06:12.720 回答