【问题标题】:Android Custom Component View w/Rounded Corners带圆角的 Android 自定义组件视图
【发布时间】:2011-04-29 01:13:15
【问题描述】:

我正在尝试创建一个带有圆角(和选择的背景颜色)的视图,我可以使用不同的背景颜色重复使用它;很难解释,所以这是我的代码:

/app/src/com/packagename/whatever/CustomDrawableView.java


package com.packagename.whatever;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.drawable.PaintDrawable;
import android.util.AttributeSet;
import android.view.View;

public class CustomDrawableView extends View {
    private PaintDrawable mDrawable;
    int radius;

    private void init(AttributeSet attrs) {
        TypedArray a = getContext().obtainStyledAttributes(attrs,R.styleable.RoundedRect);
        radius = a.getInteger(R.styleable.RoundedRect_radius, 0);
    }

    public CustomDrawableView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(attrs);

        mDrawable = new PaintDrawable();
    }

    protected void onDraw(Canvas canvas) {
        mDrawable.setCornerRadius(radius);
        mDrawable.draw(canvas);
    }
}

这是显示自定义组件的 XML: /app/res/layout/test.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ny="http://schemas.android.com/apk/res/com.packagename.whatever"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#ffffff"
    android:padding="10dp">

    <com.packagename.whatever.CustomDrawableView
        android:id="@+id/custom"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:background="#b80010"
        ny:radius="50"
    />

</LinearLayout>

我希望红色框有 50 像素的圆角,但正如您所见,它没有:

我的想法是我可以轻松地更改 XML 中的背景颜色,并自动获得一个漂亮的圆角视图,而无需创建多个可绘制对象。

感谢您的帮助!

【问题讨论】:

    标签: android android-layout


    【解决方案1】:

    您需要将圆角半径和颜色设置为背景可绘制对象。

    这是一种可行的方法。获取您在 android:background 中设置的颜色,然后使用它来创建一个新的可绘制对象,并在构造函数中将其设置为背景。只要您仅将 android:background 设置为颜色值,这将起作用。

       public CustomDrawableView(Context context, AttributeSet attrs) {
            super(context, attrs);
            init(attrs);
    
            // pull out the background color
            int color = attrs.getAttributeIntValue("http://schemas.android.com/apk/res/android", "background", 0xffffffff);
    
            // create a new background drawable, set the color and radius and set it in place
            mDrawable = new PaintDrawable();
            mDrawable.getPaint().setColor(color);
            mDrawable.setCornerRadius(radius);
            setBackgroundDrawable(mDrawable);
        }
    

    如果您覆盖 onDraw,请确保先调用 super.onDraw(canvas) 来绘制背景。

    【讨论】:

      【解决方案2】:

      给定一个像这样的简单 shapedrawable:

      public ShapeDrawable Sd(int s){
      
      float[] outerR = new float[] { 12, 12, 12, 12, 12, 12, 12, 12 };
      ShapeDrawable mDrawable = new ShapeDrawable(new RoundRectShape(outerR, null,null));
      
                  mDrawable.getPaint().setColor(s);
      return mDrawable;
      }
      

      您可以执行以下操作:

          LinearLayout l=(LinearLayout) findViewById(R.id.testLayout);
      l.setBackgroundDrawable(Sd(0xff74AC23));
      

      12 代表半径。 你可以将它应用到任何视图作为背景可绘制对象。

      【讨论】:

      • 如果您已经为按钮设置了颜色并且需要将它们转角,我使用此代码资源 res = this.getResources(); button2.setBackgroundDrawable(this.Sd(res.getColor(R.color.color1x2)));
      【解决方案3】:

      【讨论】:

      • 感谢您的回复,但我正在尝试动态设置背景颜色。您的前两个链接是我通常使用的方法,但只允许静态 bg,因此是我的问题。第三个链接可能会起作用......我会看看。 :)
      • @iamkoa 之后调用setBackgoundColor() 不起作用?
      • 是的,但这需要您添加一个 android:background 可绘制对象,然后在 Java 中手动覆盖可绘制对象的背景颜色,对吧?我正在寻找一种允许所有内容都在一个 XML 调用中的解决方案。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多