سؤال

I have a problem which I don't know how to solve. I have created a custom view and would like to align it to parrent buttom in a relative layout but when I do that the view gets transparrent?! How can this be? Also if I set the surrounding views layout_height = wrap_content it fills the whole screen. How can I configure it not to do so but just wrap content?

Here is my custom view

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.RelativeLayout;

public class HomeMenuBackground extends View
{ 
    private int mScreenWidth;
    private int mScreenHeight;
    private RectF mSpace;
    private RectF mSpaceStroke;
    private Paint mPaint;
    private Paint mPaintStroke;
    private Rect mRect; 
    private LinearGradient mGradient;

    public HomeMenuBackground(Context context, AttributeSet attrs)
    {
        super(context, attrs);

        mScreenWidth = context.getResources().getDisplayMetrics().widthPixels;
        mScreenHeight = context.getResources().getDisplayMetrics().heightPixels;
        final int mHeighMenu = (int) (mScreenHeight*0.8);
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mSpace = new RectF(-20, 2, mScreenWidth + 20, 148);
        mSpaceStroke = new RectF(-22, 0, mScreenWidth + 22, 150);
        mRect = new Rect();

        mPaintStroke = new Paint();
        mPaintStroke.setStyle(Paint.Style.STROKE);
        mPaintStroke.setStrokeCap(Paint.Cap.ROUND);
        mPaintStroke.setStrokeWidth(2);
        mPaintStroke.setColor(Color.BLACK);
        mPaintStroke = new Paint(Paint.ANTI_ALIAS_FLAG);

    }

    @Override
    public void onDraw(Canvas canvas)
    {       
        mRect.set(0, 75, canvas.getWidth(), canvas.getHeight());
        canvas.drawArc(mSpaceStroke, 180, 360, true, mPaintStroke);
        canvas.drawArc(mSpace, 180, 360, true, mPaint);
        canvas.drawRect(mRect, mPaint);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) 
    {
        if(mGradient == null)
        {
            mGradient = new LinearGradient(0, 0, 0, MeasureSpec.getSize(heightMeasureSpec), Color.WHITE, Color.GRAY, Shader.TileMode.MIRROR);
            mPaint.setShader(mGradient);
        }

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
}

I hope you can help me :D

BR FireFly3000

هل كانت مفيدة؟

المحلول

For deciding the height and width of Custom View you can override setMeasuredDimension (int measuredWidth, int measuredHeight) method and provide the height, width of view as per your requirement. And I tried with RelativeLayout I can see your Layout at the Bottom with height and width of (100, 100) by using setMeasuredDimension(100, 100); inside onMeasure() method.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top