Round Border LinerLayout

≯℡__Kan透↙ 提交于 2020-01-06 09:06:07

问题


so the question How to add border around linear layout except at the bottom? answers my question partially but i cant seem to figure out how to make the corners round ..


回答1:


  1. Create a XML file named round_border in your layout folder.

  2. Now put this code in your XML file :

    <shape xmlns:android="http://schemas.android.com/apk/res/android"> 
    <stroke android:width="4dp" android:color="#FF00FF00" /> 
    <solid android:color="#ffffff" /> 
    <padding android:left="7dp" android:top="7dp" 
            android:right="7dp" android:bottom="7dp" /> 
    <corners android:radius="10dp" /> 
    

  3. Now use this file as a background of your LinearLayout like this :

    <LinearLayout android:id="@+id/LinearLayout01"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:padding="20dip"
    android:background="@drawable/round_border">
    



回答2:


In your linear layout

   android:background="@drawable/bkg"

Define the below xml in drawable folder.

bkg.xml

<?xml version="1.0" encoding="UTF-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"> 
<solid android:color="#10EB0A"/>    
<stroke android:width="3dp"
    android:color="#0FECFF" /> 
<padding android:left="5dp"
     android:top="5dp"
     android:right="5dp"
     android:bottom="5dp"/> 
<corners android:bottomRightRadius="7dp"
     android:bottomLeftRadius="7dp" 
     android:topLeftRadius="7dp"
     android:topRightRadius="7dp"/> 
</shape>   



回答3:


this is a custom Drawable you can use:

class RoundedImageDrawable extends Drawable {

    private Bitmap mBitmap;
    private Matrix mMatrix;
    private Path mPath;
    private float mRx;
    private float mRy;

    public RoundedImageDrawable(Resources res , int id, float rx, float ry) {
        mBitmap = BitmapFactory.decodeResource(res, id);
        mMatrix = new Matrix();
        mPath = new Path();
        mRx = rx;
        mRy = ry;
    }

    @Override
    protected void onBoundsChange(Rect bounds) {
        RectF src = new RectF(0, 0, mBitmap.getWidth(), mBitmap.getHeight());
        RectF dst = new RectF(bounds);
        mMatrix.setRectToRect(src, dst, Matrix.ScaleToFit.FILL);
        mPath.addRoundRect(dst, mRx, mRy, Direction.CW);
    }

    @Override
    public void draw(Canvas canvas) {
        canvas.save();
        canvas.clipPath(mPath);
        canvas.drawBitmap(mBitmap, mMatrix, null);
        canvas.restore();
    }

    @Override
    public void setAlpha(int alpha) {
    }

    @Override
    public void setColorFilter(ColorFilter cf) {
    }

    @Override
    public int getOpacity() {
        return PixelFormat.TRANSLUCENT;
    }
}

and use it in your Activity:

LinearLayout ll = findViewById(R.id.layout);
Drawable d = new RoundedImageDrawable(getResources(), R.drawable.background, 20, 20);
ll.setBackgroundDrawable(d);


来源:https://stackoverflow.com/questions/16785081/round-border-linerlayout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!