Android 如何将圆角半径应用于 LinearLayout

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10074249/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 02:24:18  来源:igfitidea点击:

How to Apply Corner Radius to LinearLayout

androidandroid-layout

提问by Aamirkhan

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

我想制作带有圆形边框的布局。如何在 a 中应用特定大小的半径LinearLayout

回答by Siddharth Lele

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

您可以在 drawable 文件夹中创建一个 XML 文件。调用它,例如,shape.xml

In shape.xml:

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.

<corner>标签用于您的特定问题。

Make changes as required.

根据需要进行更改。

And in your whatever_layout_name.xml:

而在你的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....

这就是我通常在我的应用程序中所做的。希望这可以帮助....

回答by Mirko Lindner

You would use a Shape Drawableas the layout's background and set its cornerRadius. Check this blogfor a detailed tutorial

您将使用Shape Drawable作为布局的背景并设置其cornerRadius。 查看此博客以获取详细教程

回答by Sudhir singh

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

可绘制文件夹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>

回答by Ramesh kumar

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

试试这个,以便以编程方式将半径设置为 LinearLayout 或任何视图的背景。

 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());