一般android中,实现圆角布局只需要设置drawable文件为其背景即可, 但如果有多个圆角布局,要求只是背景色不同,我们不可能建立多个drawable 文件去代码里动态设置, 这时候我们就需要自定义一个圆角布局,在代码中设置其颜色就行。
首先,我们先来看下xml中定义圆角代码。
在drawable文件夹下建立个corner.xml文件,
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#99CCFF" />
<corners android:topLeftRadius="20dp"
android:topRightRadius="20dp"
android:bottomRightRadius="20dp"
android:bottomLeftRadius="20dp"/>
<stroke android:width="1dp" android:color="#000000" />
</shape>
接下来我们一起看下自定义圆角布局。
效果图
自定义布局代码
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;
import android.widget.RelativeLayout;
/**
* 圆角布局的实现
*/
public class RoundRelativeLayout extends RelativeLayout {
private Context mContext;
private int mBgColor = 0;
private int mCornerSize = 10;
public RoundRelativeLayout(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
}
@Override
protected void onDraw(Canvas canvas)
{
setBackgroundRounded(canvas, this.getMeasuredWidth(), this.getMeasuredHeight(), this);
super.onDraw(canvas);
}
public void showColor(int color){
mBgColor = color;
//自定义layout中,必须在invalidate前,设置layout背景色,否则,无法手动调用ondraw方法;自定义view中则无需设置背景色
this.setBackgroundColor(0x00000000);
invalidate();
}
private void setBackgroundRounded(Canvas c, int w, int h, View v){
if(w <= 0 || h <= 0){
return;
}
Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG);
paint.setAntiAlias(true);
paint.setColor(mBgColor);
RectF rec = new RectF(0, 0, w, h);
c.drawRoundRect(rec, dipToPx(mCornerSize), dipToPx(mCornerSize), paint);
}
private int dipToPx(int dip) {
float scale = getContext().getResources().getDisplayMetrics().density;
return (int) (dip * scale + 0.5f * (dip >= 0 ? 1 : -1));
}
}
外部xml文件引用
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<com.example.cp.mileager.View.RoundRelativeLayout
android:id="@+id/rl_all"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:gravity="bottom"
android:padding="20dp"
>
<TextView
android:id="@+id/tv_air"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#fff"
android:textSize="18sp" >
</TextView>
<TextView
android:id="@+id/tv_card"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#fff"
android:layout_toRightOf="@+id/tv_air"
android:layout_marginLeft="15dp"
android:layout_alignBaseline="@+id/tv_air"
android:textSize="14sp" >
</TextView>
<ImageView
android:id="@+id/iv_add_card"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/icon_add"
android:layout_alignParentRight="true"
android:scaleType="center"
/>
</com.example.cp.mileager.View.RoundRelativeLayout>
</RelativeLayout>
外部java代码调用
viewHolder.rlCard = (RoundRelativeLayout)convertView.findViewById(R.id.rl_all);
viewHolder.rlCard.showColor(0xffff0000); // argb 一定为8位