How to Apply Corner Radius to LinearLayout

前端 未结 4 1861
傲寒
傲寒 2020-11-29 16:37

I want to make a layout with a rounded border. How can I apply a radius of a particular size in a LinearLayout?

相关标签:
4条回答
  • 2020-11-29 16:59

    You would use a Shape Drawable as the layout's background and set its cornerRadius. Check this blog for a detailed tutorial

    0 讨论(0)
  • 2020-11-29 17:03

    try this, for Programmatically to set a background with radius to LinearLayout or any View.

     private Drawable getDrawableWithRadius() {
    
        GradientDrawable gradientDrawable   =   new GradientDrawable();
        gradientDrawable.setCornerRadii(new float[]{20, 20, 20, 20, 20, 20, 20, 20});
        gradientDrawable.setColor(Color.RED);
        return gradientDrawable;
    }
    
    LinearLayout layout = new LinearLayout(this);
    layout.setBackground(getDrawableWithRadius());
    
    0 讨论(0)
  • 2020-11-29 17:16

    Layout

    <LinearLayout 
        android:id="@+id/linearLayout"
        android:layout_width="300dp"
        android:gravity="center"
        android:layout_height="300dp"
        android:layout_centerInParent="true"
        android:background="@drawable/rounded_edge">
     </LinearLayout>
    

    Drawable folder rounded_edge.xml

    <shape 
    xmlns:android="http://schemas.android.com/apk/res/android">
        <solid 
            android:color="@android:color/darker_gray">
        </solid>
        <stroke 
             android:width="0dp" 
             android:color="#424242">
        </stroke>
        <corners 
             android:topLeftRadius="100dip"
             android:topRightRadius="100dip"
             android:bottomLeftRadius="100dip"
             android:bottomRightRadius="100dip">
        </corners>
    </shape>
    
    0 讨论(0)
  • 2020-11-29 17:24

    You can create an XML file in the drawable folder. Call it, for example, shape.xml

    In shape.xml:

    <shape
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle"   >
    
        <solid
            android:color="#888888" >
        </solid>
    
        <stroke
            android:width="2dp"
            android:color="#C4CDE0" >
        </stroke>
    
        <padding
            android:left="5dp"
            android:top="5dp"
            android:right="5dp"
            android:bottom="5dp"    >
        </padding>
    
        <corners
            android:radius="11dp"   >
        </corners>
    
    </shape>
    

    The <corner> tag is for your specific question.

    Make changes as required.

    And in your whatever_layout_name.xml:

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_margin="5dp"
        android:background="@drawable/shape"    >
    </LinearLayout>
    

    This is what I usually do in my apps. Hope this helps....

    0 讨论(0)
提交回复
热议问题