136

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

4

5 回答 5

307

您可以在 drawable 文件夹中创建 XML 文件。例如,调用它,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>

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

根据需要进行更改。

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

这是我通常在我的应用程序中所做的。

于 2012-04-09T14:20:17.543 回答
11

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

于 2012-04-09T13:57:50.650 回答
11

布局

<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>

可绘制文件夹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>
于 2018-03-17T07:40:47.170 回答
7

试试这个,以编程方式将具有半径的背景设置为 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());
于 2018-10-04T13:41:51.573 回答
0

尝试使用 Glide 模块来处理图像。你需要像这样实现它:

implementation 'com.github.bumptech.glide:glide:4.12.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.12.0'

Here is my use case in a Fragment. This is where I get the URL of image from the constant. Then I set the corner radius to 16. And then I set the resulting background in my LinearLayout:

        Glide.with(this).load(Constants.FLAT_IMAGE).apply(RequestOptions.bitmapTransform(
        RoundedCorners(16)
    )).into(object :
        CustomTarget<Drawable>() {
        override fun onLoadCleared(placeholder: Drawable?) {
            super.onLoadStarted(placeholder)
        }

        override fun onResourceReady(
            resource: Drawable,
            transition: Transition<in Drawable>?
        ) {
            binding.llPromoDreamFlatImage.background = resource
        }
    })

This way you can set the background image as a picture or a color with rounded edges for the LinearLayout. Here is my example:

        <LinearLayout
        android:id="@+id/llPromoDreamFlatImage"
        android:layout_width="match_parent"
        android:layout_height="420dp"
        android:layout_marginTop="35dp"
        android:layout_gravity="center"
        android:gravity="center"
        >
于 2021-07-16T09:33:15.613 回答