【问题标题】:Arc Shape Background Android弧形背景Android
【发布时间】:2015-08-09 10:36:30
【问题描述】:

我想创建一个带有弧形背景的视图..我的视图的权重为 .3.. 这使它占据了我布局的三分之一..

我尝试将其背景设置为弧形(我希望它是半椭圆形),如下所示:

    ArcShape shape = new ArcShape(270, 180);
    ShapeDrawable shapeDrawable = new ShapeDrawable(shape);
    shapeDrawable.getPaint().setColor(getResources().getColor(R.color.primary_color));
    leftPathView.setBackgroundDrawable(shapeDrawable);

这会生成我需要的形状,但它并没有填满我的视图空间。它只需要它的一半。但是当我创建一个完整的圆圈时,它会占据所有空间。

有什么想法吗?

【问题讨论】:

标签: java android xml android-shape


【解决方案1】:

经过一番研究,我发现这是最好的答案。特别是如果您想在 onCreate 函数中执行此操作,因为当 onCreate 函数尚未结束时,布局还没有宽度和高度 (more info here)。

final LinearLayout leftPathView= (LinearLayout) findViewById(R.id.left_path_view);

layout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
        leftPathView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
        leftPathView.setX(-layout.getWidth());
        leftPathView.getLayoutParams().width = layout.getWidth() * 2;
        leftPathView.requestLayout();
    }
});

ArcShape shape = new ArcShape(270, 180);
ShapeDrawable shapeDrawable = new ShapeDrawable(shape);
shapeDrawable.getPaint().setColor(getResources().getColor(R.color.primary_color));
layout.setBackground(shapeDrawable);

此代码确实使您的视图超出了屏幕的范围,因此在向此视图添加其他项目时,您需要考虑到这一点。一种解决方案是将 View 放在 RelativeLayout 中,并将另一个视图放在 leftPathView 的顶部。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多