簡體   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