2011-04-11 79 views
90

我需要在Android UI中绘制一个圆角矩形。对于TextViewEditText具有相同的圆角矩形也是有帮助的。如何在Android UI中绘制圆角矩形?

+0

看一看这个讨论HTTP创建一个新的XML:// stackoverflow.com/questions/3646415/how-to-create-edittext-with-rounded-corners – 2011-04-11 08:08:45

回答

147

在你的布局XML执行以下操作:

<shape xmlns:android="http://schemas.android.com/apk/res/android"> 
     <gradient 
      android:endColor="@color/something" 
      android:centerColor="@color/something_else" 
      android:startColor="@color/something_else_still" 
      android:angle="270" /> 
     <corners 
      android:radius="3dp" /> 
     <padding 
      android:left="10dp" 
      android:top="10dp" 
      android:right="10dp" 
      android:bottom="10dp" /> 
    </shape> 

通过改变机器人:半径可以改变“四舍五入”的量的角落。

1

如果你想使用圆角矩形作为TextView和EditText的背景,你应该使用自定义背景。您需要使用形状部件此获取更多信息阅读这个问题Using shape drawable as my background xml

17

monodroid,你可以这样做圆角矩形,然后保持这个父类,editbox和其他布局功能可以添加。

class CustomeView : TextView 
    { 
     public CustomeView (Context context, IAttributeSet) : base (context, attrs) 
     { 
     } 
     public CustomeView(Context context, IAttributeSet attrs, int defStyle) : base(context, attrs, defStyle) 
     { 
     } 
     protected override void OnDraw(Android.Graphics.Canvas canvas) 
     { 
      base.OnDraw(canvas); 
      Paint p = new Paint(); 
      p.Color = Color.White; 
      canvas.DrawColor(Color.DarkOrange); 

      Rect rect = new Rect(0,0,3,3); 

      RectF rectF = new RectF(rect); 


      canvas.DrawRoundRect(rectF, 1,1, p); 



     } 
    } 
} 
94

我想,这是你确切需要的。

这里创建圆角矩形的drawable(xml)文件。 round_rect_shape.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle" > 

    <solid android:color="#ffffff" /> 

    <corners 
     android:bottomLeftRadius="8dp" 
     android:bottomRightRadius="8dp" 
     android:topLeftRadius="8dp" 
     android:topRightRadius="8dp" /> 

</shape> 

这里布局文件:my_layout.xml

<LinearLayout 
    android:id="@+id/linearLayout1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="@drawable/round_rect_shape" 
    android:orientation="vertical" 
    android:padding="5dp" > 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Something text" 
     android:textAppearance="?android:attr/textAppearanceLarge" 
     android:textColor="#ff0000" /> 

    <EditText 
     android:id="@+id/editText1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" > 

     <requestFocus /> 
    </EditText> 
</LinearLayout> 

- >在上面的代码中,具有背景的LinearLayout(即放置到创建密钥的作用圆角矩形)。 因此,您可以将任何视图像TextView,EditText ...放入该LinearLayout中,以便将所有背景视为圆形矩形。现在

+1

有没有办法来抽象呢?我希望能够在我的styles.xml中使用'android:background =“@ drawable/round_rect_shape”',但通过设置另一个属性来使用不同的背景颜色。除了为每种颜色创建相同的drawable外,是否还有其他选项? – karl 2013-12-17 17:52:39

0
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:padding="10dp" 
    android:shape="rectangle"> 
    <solid android:color="@color/colorAccent" /> 
    <corners 
     android:bottomLeftRadius="500dp" 
     android:bottomRightRadius="500dp" 
     android:topLeftRadius="500dp" 
     android:topRightRadius="500dp" /> 
</shape> 

,其中元素要使用这种形状只需要添加: android:background="@drawable/custom_round_ui_shape"

在绘制名为“custom_round_ui_shape”