如何在自定义ViewGroup中膨胀RelativeLayout

发布于 2024-11-16 04:45:55 字数 2535 浏览 4 评论 0原文

我有一个自定义动画 ViewGroup (很大程度上基于 滑动选项卡)。

在我的应用程序中,我将其简化为单个 TextView,其行为就像 SlidingTab(锁定屏幕上的滑块)。这很好用,但是滑动的 TextView 是在代码中定义的。我想要一个完整的相对布局来滑动。

我查看了各种帖子尝试不同的事情< /a> 但仍然没有让它发挥作用。必须在 Slider 构造函数中扩充relativelayout xml 文件并将其添加为“parent”的子级,但我还需要能够执行measure() 和layout() 方法。请参阅下面的相关代码。

公共类 SlindingView 扩展 ViewGroup {

private static Slider mLeftSlider;
private static int tabWidth;

/**
 * Simple container class for all things pertinent to a slider.
 */
private static class Slider {

    private final TextView tab;

    Slider(ViewGroup parent) {
        // Want to change 'tab' to a RelativeLayout in xml
        tab = new TextView(parent.getContext());
        tab.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.FILL_PARENT));
        tab.setText("SLIDE THIS");

        parent.addView(tab);
    }

    void layout(int l, int t, int r, int b) {
        tabWidth = getTabWidth();
        int tabHeight = getTabHeight();
        tab.layout(0, 0, tabWidth, tabHeight);
    }

    public void measure() {
        tab.measure(View.MeasureSpec.makeMeasureSpec(0,
                View.MeasureSpec.UNSPECIFIED), View.MeasureSpec
                .makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
    }

    public int getTabWidth() {
        return tab.getMeasuredWidth();
    }

    public int getTabHeight() {
        return tab.getMeasuredHeight();
    }
}

/**
 * Constructor
 */
public SlindingView(Context context, AttributeSet attrs) {
    super(context, attrs);
    mLeftSlider = new Slider(this);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);

    mLeftSlider.measure();
    final int leftTabWidth = mLeftSlider.getTabWidth();
    final int leftTabHeight = mLeftSlider.getTabHeight();
    final int width = Math.max(widthSpecSize, leftTabWidth);
    final int height = leftTabHeight;

    setMeasuredDimension(width, height);
}

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
    mLeftSlider.layout(l, t, r, b);
}

}

I have a custom animated ViewGroup (based heavily on SlidingTab).

In my app, I've stripped this down to just a single TextView that behaves just like the SlidingTab (the slider on your lock screen). This works great, but the TextView that slides is defined in code. I'd like to have an entire RelativeLayout that slides instead.

I have looked at all sorts of posts trying different things but still haven't gotten it to work. The RelativeLayout xml file has to be inflated and added as a child of 'parent' in the Slider constructer, but I also need to be able do the measure() and layout() methods. See the relevant code below.

public class SlindingView extends ViewGroup {

private static Slider mLeftSlider;
private static int tabWidth;

/**
 * Simple container class for all things pertinent to a slider.
 */
private static class Slider {

    private final TextView tab;

    Slider(ViewGroup parent) {
        // Want to change 'tab' to a RelativeLayout in xml
        tab = new TextView(parent.getContext());
        tab.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.FILL_PARENT));
        tab.setText("SLIDE THIS");

        parent.addView(tab);
    }

    void layout(int l, int t, int r, int b) {
        tabWidth = getTabWidth();
        int tabHeight = getTabHeight();
        tab.layout(0, 0, tabWidth, tabHeight);
    }

    public void measure() {
        tab.measure(View.MeasureSpec.makeMeasureSpec(0,
                View.MeasureSpec.UNSPECIFIED), View.MeasureSpec
                .makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
    }

    public int getTabWidth() {
        return tab.getMeasuredWidth();
    }

    public int getTabHeight() {
        return tab.getMeasuredHeight();
    }
}

/**
 * Constructor
 */
public SlindingView(Context context, AttributeSet attrs) {
    super(context, attrs);
    mLeftSlider = new Slider(this);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);

    mLeftSlider.measure();
    final int leftTabWidth = mLeftSlider.getTabWidth();
    final int leftTabHeight = mLeftSlider.getTabHeight();
    final int width = Math.max(widthSpecSize, leftTabWidth);
    final int height = leftTabHeight;

    setMeasuredDimension(width, height);
}

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
    mLeftSlider.layout(l, t, r, b);
}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文