Round Border LinearLayout

时间:2013-05-28 06:17:59

标签: android android-layout android-linearlayout android-xml

所以问题How to add border around linear layout except at the bottom?部分地回答了我的问题,但我似乎无法弄清楚如何使角落成为圆角..

3 个答案:

答案 0 :(得分:1)

在线性布局中

   android:background="@drawable/bkg"

在drawable文件夹中定义以下xml。

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>   

答案 1 :(得分:1)

  1. 在布局文件夹中创建名为round_border的XML文件。

  2. 现在将此代码放入XML文件中:

    <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. 现在使用此文件作为LinearLayout的背景,如下所示:

    <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 :(得分:0)

这是您可以使用的自定义Drawable:

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;
    }
}

并在您的活动中使用它:

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