Android:带位图的对话框背景,带边框的圆角

时间:2014-09-01 17:50:00

标签: android layout background dialog

在一个对话框中,我希望它有一个背景,带有3dp绿色圆角边框。我已经实现了以下内容:

对话框

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="30dp"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:layout_marginTop="30dp"
    android:background="@drawable/layout_round_corners_image"
    android:splitMotionEvents="false" >
    .....
</RelativeLayout>

layout_round_corners_image.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

    <item android:drawable="@drawable/strip1">
        <shape
            android:shape="rectangle" >
            <solid android:color="@android:color/transparent" />

            <stroke
                android:width="3dp"
                android:color="@color/green" />

            <corners android:radius="35px" />

            <padding
                android:bottom="0dp"
                android:left="0dp"
                android:right="0dp"
                android:top="0dp" />
        </shape>
    </item>

</layer-list>

问题:

对话框现在没有显示任何图像(strip1),只有一个带有3dp绿色圆角边框的黑色(空白)背景。

图像如何成为对话框的背景,其角落在绿色圆角边框内调整?

谢谢!

1 个答案:

答案 0 :(得分:0)

它的工作原理是创建一个新类,编码如下:

新类:

public class RoundedImageView extends ImageView {

      public RoundedImageView(Context context) {
           super(context);
      }

      public RoundedImageView(Context context, AttributeSet attrs) {
           super(context, attrs);
      }

      public RoundedImageView(Context context, AttributeSet attrs, int defStyle) {
           super(context, attrs, defStyle);
      }


      @Override
      protected void onDraw(Canvas canvas) {
           int radius = (int) getResources().getDimension(R.dimen.corner_radius); // angle of round corners
           Path clipPath = new Path();
           RectF rect = new RectF(0, 0, this.getWidth(), this.getHeight());
           clipPath.addRoundRect(rect, radius, radius, Path.Direction.CW);
           canvas.clipPath(clipPath);
           super.onDraw(canvas);
       }
    }

然后在layout.xml中,用RoundedImageView替换ImageView:

    <com.example.abc.RoundedImageView
        android:id="@+id/bgd_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_margin="@dimen/dp2"
        android:scaleType="fitXY"
        android:src="@drawable/strip1" />