繁体   English   中英

弧形背景Android

[英]Arc Shape Background Android

我想创建一个具有Arc Shape背景的视图。我的视图的权重为0.3。这使其占据了布局的三分之一。

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

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

这会生成我需要的形状,但不会填充视图的空间。仅占一半。但是当我创建一个完整的圆时,它将占据所有空间。

任何想法 ?

经过一些研究,我发现这是最好的答案。 特别是如果您想在onCreate函数中执行此操作,则因为onCreate函数尚未结束,布局还没有宽度和高度( 更多信息 ,请参见此处 )。

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超出了屏幕的范围,因此在向此View添加其他项时,您需要考虑到这一点。 一种解决方案是将View放在RelativeLayout中,然后在leftPathView顶部放置另一个视图。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM