android - 挂接 LayoutInflater 并在 inflatedView 时更改值

标签 android layout-inflater

我想实现一个自定义的 LayoutInflater 来缩放维度值,例如, 使用:

int scale = 2;
MyInflater.inflate(R.id.testview, null, parent, scale);

将膨胀所有维度值加倍的 xml。

也就是像这样的xml:

<View android:layout_width="10dp" android:layout_height="10dp" />

将膨胀为宽度和高度为 20dp 的 View 。

LayoutInflater.Factory 无法解决我的问题。

有什么方法可以实现吗?

最佳答案

也许您可以使用此代码循环展开布局的所有子项,并通过设置 LayoutParams 来乘以宽度和高度:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my);

    MyInflater inflater = new MyInflater(LayoutInflater.from(this), this);
    ViewGroup viewGroup = (ViewGroup) findViewById(R.id.my_layout);
    inflater.inflate(R.layout.test_view, viewGroup, true, 2);
}

private class MyInflater extends LayoutInflater {

    private int mScale;

    protected MyInflater(LayoutInflater original, Context newContext) {
        super(original, newContext);
    }

    @Override
    public LayoutInflater cloneInContext(Context newContext) {
        return null;
    }

    public View inflate(int resource, ViewGroup root, boolean attachToRoot, int scale) {
        mScale = scale;
        // This will return the parent of the inflated resource (if it has one)
        View viewRoot = super.inflate(resource, root, attachToRoot);

        if (viewRoot instanceof ViewGroup) {
            loopViewGroup((ViewGroup) viewRoot, viewRoot == root);
        } else {
            doubleDimensions(viewRoot);
        }
        return viewRoot;
    }

    private void doubleDimensions(View view) {
        ViewGroup.LayoutParams params = view.getLayoutParams();
        Log.d(TAG, "Before => "+params.width+" : "+params.height);
        params.width = params.width * mScale;
        params.height = params.height * mScale;
        Log.d(TAG, "After => "+params.width+" : "+params.height);
        view.setLayoutParams(params);
    }

    private void loopViewGroup(ViewGroup group, boolean isRoot) {
        // If viewRoot == root, skip setting ViewGroup params
        if (!isRoot) doubleDimensions(group);

        // Loop the ViewGroup children
        for (int i=0; i<group.getChildCount(); i++) {
            View child = group.getChildAt(i);
            // If a child is another ViewGroup, loop it too
            if (child instanceof ViewGroup) {
                loopViewGroup((ViewGroup) child, false);
            } else {
                doubleDimensions(child);
            }
        }
    }
}

关于android - 挂接 LayoutInflater 并在 inflatedView 时更改值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25997883/

相关文章:

java - 图像查看器中每个图像的不同文本

android - 加载布局后如何知道 View 元素?

android - 当 mediaPlayer 准备播放时如何显示进度条

Lollipop 上的 Android Stringblock.get NullPointer 异常

java - 在 Android 中获取 LayoutInflater 的正确方法是什么?

android - 表情符号(表情) View /键盘布局的实现

android - fragment 内的 onCreateOptionsMenu

android - 通过滚动同步 2 个 ListView

android - Android 上的 Chrome 不考虑链接点击的 z-index

java - 错误的 xml block 3 线性布局和 2 日历