简体   繁体   English

Android:调整customview的高度

[英]Android:adjust height of customview

I have created a Circular customview as shown in screenShot.As you can see the lower part of the circle is sliced in the layout.How can i adjust its height. 我已经创建了一个圆的customview,如screenShot所示。您可以看到圆圈的下部在布局中被切片了,如何调整其高度。 Here is the relevant code 这是相关的代码

CustomView.class CustomView.class

protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    float width = (float)getWidth();
    float height = (float)getHeight();
    float radius;
    if (width > height){
        radius = height/2;
    }else{
        radius = width/2;
    }

    Path path = new Path();
    path.addCircle(width/2, height/2, radius,Path.Direction.CW);

    Paint paint = new Paint();
    //change color of arc
    paint.setColor(Color.parseColor("#d50000"));
    paint.setStrokeWidth(5);
    paint.setFlags(Paint.ANTI_ALIAS_FLAG);
    paint.setStyle(Paint.Style.FILL);

    float center_x, center_y;
    final RectF oval = new RectF();

    paint.setStyle(Paint.Style.STROKE);
    center_x = width/2;
    center_y = height/2;

    oval.set(center_x - radius,center_y - radius,center_x + radius,center_y + radius);
    canvas.drawArc(oval, 270, 270, false, paint);
}

layout.xml layout.xml

<com.gosemathraj.CustomSemicircleUp
    android:layout_height="@dimen/semicircle" // 86dp
    android:layout_width="wrap_content" />

screenshot 屏幕截图

在此处输入图片说明

You can try to wrap the CustomSemicircleUp inside a LinearLayout (or any layout) and then center the circle view, so the aspect ratio is kept for the original view: 您可以尝试将CustomSemicircleUp包装在LinearLayout(或任何布局)中,然后将圆形视图居中,以便为原始视图保留长宽比:

<LinearLayout
     xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
     android:layout_height="match_parent">

     <com.gosemathraj.CustomSemicircleUp
         android:layout_gravity="center"
         android:layout_height="@dimen/semicircle" // 86dp
         android:layout_width="wrap_content" />

</LinearLayout>

But the android:layout_width="wrap_content" is still problematic because of different screen sizes. 但是android:layout_width =“ wrap_content”仍然存在问题,因为屏幕尺寸不同。 You will then have get the screen width and set the height accordingly. 然后,您将获得屏幕宽度并相应地设置高度。

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

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