【问题标题】:hook LayoutInflater and change values when inflating views在膨胀视图时挂钩 LayoutInflater 并更改值
【发布时间】:2014-09-23 14:36:46
【问题描述】:

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

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

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

就是这样一个xml:

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

将膨胀到宽度和高度为 20dp 的视图。

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

有什么方法可以实现吗?

【问题讨论】:

  • "LayoutInflater.Factory 无法解决我的问题。" -- 为什么不呢?
  • @CommonsWare 如果使用LayoutInflater.Factory,我必须自己创建每个视图,因为没有链接超级调用;而LayoutParams的生成是出自createViewFromTag(),这里调用的是Factory的onCreateView(),所以在Factory的onCreateView()中,修改LayoutParams是不可能的。
  • 我不太明白这个问题,你想在膨胀时从 xml 中缩放视图?
  • @DarkoPetkovski yepp,我想在放大视图时缩放 dp 中的所有尺寸

标签: android layout-inflater


【解决方案1】:

也许您可以使用此代码循环膨胀布局的所有子级,并通过设置 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);
            }
        }
    }
}

【讨论】:

  • 这会解决问题,但可能有性能问题?我想要 ALL 尺寸缩放,所以我正在考虑在膨胀时修改 xml attrs。
  • 这种方法可以作为备用计划。点赞,谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-12
  • 1970-01-01
相关资源
最近更新 更多