在setContentView()之前检索自定义布局尺寸

时间:2014-04-20 19:25:02

标签: android eclipse android-custom-view

我正在尝试绘制自定义视图并将其放在滚动视图中。我这样做的方法是定义自定义视图并将其添加到线性布局中,线性布局本身位于滚动视图内。

问题:当我尝试将自定义视图添加到onCreate()中的线性布局时,我必须使用addView()命令发送自定义视图的尺寸。我不知道自定义视图将在不同设备上占用多少空间,因此我无法发送任何值,除非它们是我想要避免的显式整数值,因为我希望View能够适应不同的设备

问题:如何在运行setContentView()之前获取自定义视图的尺寸或未来尺寸?

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.Display;
import android.view.Menu;
import android.view.View;
import android.widget.LinearLayout;

public class MainActivity extends Activity {

    int screenWidth;
    int screenHeight;

    int viewWidth;
    int viewHeight;

    public class DrawView extends View {

        Rect[][] ctgRect = new Rect[3][11];

        Paint whitePaint;
        Paint blackPaint;

        int ctgRectWidth;
        int ctgRectHeight;
        int padding;

        public DrawView(Context context) {
            super(context);
            // TODO Auto-generated constructor stub
        }

        @Override
        protected void onDraw(Canvas canvas) {
            // TODO Auto-generated method stub
            super.onDraw(canvas);

            setVariables(canvas);
            setPaint();

            canvas.drawColor(Color.WHITE);

            drawRects(canvas);

        }

        protected void drawRects(Canvas canvas) { //Irrelevant to question          
            int currentPixelPos = 0;

            for (int x = 1; x < 11; x++) {
                canvas.drawRect(0 + padding, currentPixelPos + padding, 0 + padding + ctgRectWidth, 
                        currentPixelPos + padding + ctgRectHeight, blackPaint);

                currentPixelPos += (padding + ctgRectHeight);
            }

            currentPixelPos = 0;

            for (int x = 1; x < 11; x++) {
                canvas.drawRect(0 + padding + ctgRectWidth + padding, currentPixelPos + padding, 
                        screenWidth - padding, 
                        currentPixelPos + padding + ctgRectHeight, blackPaint);

                currentPixelPos += (padding + ctgRectHeight);
            }
        }

        protected void setVariables(Canvas canvas) { //Irrelevant to question
            screenWidth = canvas.getWidth();
            screenHeight = canvas.getHeight();

            padding = (int) (canvas.getWidth() * 0.0085);
            ctgRectWidth =  (int) ((screenWidth - (padding * 3)) / 2);
            ctgRectHeight = (int) ((ctgRectWidth / 2));
        }

        protected void setPaint() { //Irrelevant to question
            whitePaint = new Paint();
            whitePaint.setColor(Color.WHITE);

            blackPaint = new Paint();
            blackPaint.setColor(Color.BLACK);
        }

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(new DrawView(this));

        setContentView(R.layout.activity_main); 

        LinearLayout layout = (LinearLayout) findViewById(R.id.mainLinearLayout);
        DrawView customView = new DrawView(this);
        layout.addView(customView, questionVariableWidth, questionVariableHeight);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

感谢任何回复的人。

1 个答案:

答案 0 :(得分:0)

感谢@XaverKapeller的回复,但我找到了解决方案。如果其他人遇到此问题:我计算了屏幕宽度并使用它来调整自定义视图的大小。

我附上了我以前做过的功能。变量是不言自明的。

public void setDimensionVars() { //Retrieve/set dimensions for components
    DisplayMetrics displaySpecs = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(displaySpecs);

    screenWidth = displaySpecs.widthPixels;
    screenHeight = displaySpecs.heightPixels;
    padding = (int) (screenWidth * 0.0085);

    rectWidth = (int) ((screenWidth - (padding * 3)) / 2);
    rectHeight = (int) (rectWidth / 2);

    viewWidth = screenWidth;
    viewHeight = (0 + padding + rectHeight) * ((int) (Math.ceil((float) NUM_OF_CATEGORIES / 2)));
}

希望它有所帮助。