Android自定义视图onSizeChanged单位?

时间:2017-04-10 04:22:02

标签: android layout android-custom-view units-of-measurement

我正在创建Android自定义视图并覆盖视图

void onSizeChanged(int w,int h,int oldw,int oldh)

当我使用值创建布局时,w和h的值为300:

机器人:layout_width =" 100dp" 机器人:layout_height =" 100dp"

我无法找到有关用于w和h参数的单位的文档。它们是像素吗?

2 个答案:

答案 0 :(得分:0)

是的,它们以像素为单位,您可以将dps转换为像素供您使用。

 if (bytes.length > 0) {
            try {
                createCachedFile(this, attachmentFileName, bytes);
                this.startActivity(getSendEmailIntent(this, "tomailid", "Test", "See attached", attachmentFileName));
            } catch (IOException e) {
                e.printStackTrace();
            } catch (ActivityNotFoundException e) {
                Toast.makeText(this, "Gmail is not available on this device.", Toast.LENGTH_SHORT).show();
            }
        }

public  void createCachedFile(Context context, String fileName, byte[] content) throws IOException {
    File cacheFile = new File(context.getCacheDir() + File.separator + fileName);
    cacheFile.createNewFile();
    BufferedOutputStream bostream = new BufferedOutputStream(new FileOutputStream(cacheFile));
    bostream.write(content);
    bostream.flush();
    bostream.close();
}

public static Intent getSendEmailIntent(Context context, String email, String subject, String body, String fileName) {
    Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
    //Explicitly only use Gmail to send
   // emailIntent.setClassName("com.google.android.gm","com.google.android.gm.ComposeActivityGmail");
    //emailIntent.setType("message/rfc822");
    emailIntent.setType("vnd.android.cursor.item/email");
    //Add the recipients
    emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[] { email });
    emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
    emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, body);
    //Add the attachment to custom ContentProvider
    emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("content://" + AttachFileProvider.AUTHORITY + "/" + fileName));
    return emailIntent;
}

通过这种方式,您可以获得具有所需dp的像素,并相应地在代码中使用它们。希望这会有所帮助:)

答案 1 :(得分:0)

调用此函数时,视图大小已更改 如果更改显示尺寸代码,则可以使用此函数的高度和宽度,并将这些参数用作新尺寸。

就像下面的代码一样

    @Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    super.onSizeChanged(w, h, oldw, oldh);
    if (mHeight == 0 && mWidth == 0 && w != 0 && h != 0) {
        mHeight = getHeight();
        mWidth = getWidth();
    }
}

现在,无论高度和宽度如何变化,我都可以使用它并享受它:):)

相关问题