0

所以问题如何在除底部之外的线性布局周围添加边框?部分回答了我的问题,但我似乎无法弄清楚如何使角落变圆..

4

3 回答 3

1

在您的线性布局中

   android:background="@drawable/bkg"

在可绘制文件夹中定义以下 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>   
于 2013-05-28T06:22:51.350 回答
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">
    
于 2013-05-28T06:23:04.153 回答
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);
于 2013-05-28T07:58:23.317 回答