Programmaticaly充气查看与pre定义的措施 [英] Programmaticaly Inflate View with pre-defined measures

查看:279
本文介绍了Programmaticaly充气查看与pre定义的措施的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有这样的资源布局和我想用的布局和heigth膨胀吧:

So I have a layout resource like this and a I want to inflate it with the layout with and heigth:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="75px"
    android:layout_height="25px">

    <TextView
        android:id="@+id/drawable_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:text="Name" />

    <TextView
        android:id="@+id/drawable_description"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@id/drawable_name"
        android:layout_alignParentLeft="true"
        android:gravity="center_horizontal"
        android:layout_below="@+id/drawable_name"
        android:text="Some Text" />

</RelativeLayout>

视图可以是任何东西,而且我将其转换为位图。

The View could be anything, and I'm converting it to Bitmap.

private Bitmap getBitmap(View v){
    v.measure(MeasureSpec.makeMeasureSpec(mDrawableWidth, MeasureSpec.EXACTLY),
              MeasureSpec.makeMeasureSpec(mDrawableHeight, MeasureSpec.EXACTLY));
    v.layout(0, 0, mDrawableWidth, mDrawableHeight);
    Bitmap returnedBitmap = Bitmap.createBitmap(mDrawableWidth, mDrawableHeight,Bitmap.Config.ARGB_8888);
    Canvas c=new Canvas(returnedBitmap);
    v.draw(c);
    return returnedBitmap;
}

到现在为止,我有宽度和高度很难$ C $的CD,我希望能够做到这一点编程,访问 layout_width layout_height
有什么办法来实现这一目标?
如果有夸大这种价值观认为,另一种方式而不措施指定它们,请让我知道。

Until now, I have the Width and Height hardcoded and I want to be able to do it programatically, accessing the layout_width and layout_height. Is there any way to achieve this? If there's another way of inflating the view with this values without specifying them in the measure, please let me know.

如果我创建一个自定义视图,是否有指定固定的宽度和高度的任何机会?

If I create a Custom View, is there any chance of specifying the fixed width and height?

谢谢,迪奥戈。

推荐答案

这个例子应该工作。可能需要一些调整,以得到它完美的,根据您的需求,但给它一个镜头:

This example should work. Might take some tweaking to get it perfect, depending on your needs, but give it a shot:

//Get a bitmap from a layout resource. Inflates it into a discarded LinearLayout
//so that the LayoutParams are preserved
public static Bitmap getLayoutBitmap (Context c, int layoutRes, int maxWidth, int maxHeight) {
    View view = LayoutInflater.from(c).inflate(layoutRes, new LinearLayout(c), false);
    return getViewBitmap(view, maxWidth, maxHeight);
}

public static Bitmap getViewBitmap (View v, int maxWidth, int maxHeight) {
    ViewGroup.LayoutParams vParams = v.getLayoutParams();

    //If the View hasn't been attached to a layout, or had LayoutParams set
    //return null, or handle this case however you want
    if (vParams == null) {
        return null;
    }

    int wSpec = measureSpecFromDimension(vParams.width, maxWidth);
    int hSpec = measureSpecFromDimension(vParams.height, maxHeight);

    v.measure(wSpec, hSpec);

    final int width = v.getMeasuredWidth();
    final int height = v.getMeasuredHeight();

    //Cannot make a zero-width or zero-height bitmap
    if (width == 0 || height == 0) {
        return null;
    }

    v.layout(0, 0, width, height);

    Bitmap result = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(result);
    v.draw(canvas);

    return result;
}

private static int measureSpecFromDimension (int dimension, int maxDimension) {
    switch (dimension) {
        case ViewGroup.LayoutParams.MATCH_PARENT:
            return View.MeasureSpec.makeMeasureSpec(maxDimension, View.MeasureSpec.EXACTLY);
        case ViewGroup.LayoutParams.WRAP_CONTENT:
            return View.MeasureSpec.makeMeasureSpec(maxDimension, View.MeasureSpec.AT_MOST);
        default:
            return View.MeasureSpec.makeMeasureSpec(dimension, View.MeasureSpec.EXACTLY);
    }
}

这篇关于Programmaticaly充气查看与pre定义的措施的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆