【问题标题】:How to Apply Corner Radius to LinearLayout如何将角半径应用于线性布局
【发布时间】:2012-04-21 21:17:58
【问题描述】:

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

【问题讨论】:

标签: android android-layout


【解决方案1】:

您可以在 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>

&lt;corner&gt; 标签适用于您的具体问题。

根据需要进行更改。

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

这是我通常在我的应用程序中执行的操作。希望这会有所帮助....

【讨论】:

  • 如何在这个xml中设置背景图片
  • @vignesh:哪个可绘制对象并将其设置在哪里?如果您指的是 &lt;shape&gt; 示例,它已经在此处的布局 XML 中设置:android:background="@drawable/shape"
  • 如果这个线性布局已经有背景图片并且我希望它有一个圆角半径怎么办?在您的代码中,我将无法设置背景图像,因为 linearLayout 背景属性是使用 shape.xml 设置的
  • @MrAppleBR:我无法设置背景图片:正确。但在问题的背景下,OP 有一个有效的用例。在您提到的用例中,这不是您应该追求的。
  • @SiddharthLele 我应该去做什么?你能用一点来源或链接解释一下吗?谢谢!
【解决方案2】:

您将使用Shape Drawable 作为布局的背景并设置其cornerRadius。 Check this blog获取详细教程

【讨论】:

    【解决方案3】:

    布局

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

    【讨论】:

      【解决方案4】:

      试试这个,以编程方式将具有半径的背景设置为 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());
      

      【讨论】:

        【解决方案5】:

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

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

        这是我在片段中的用例。这是我从常量中获取图像 URL 的地方。然后我将圆角半径设置为 16。然后在我的 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
                }
            })
        

        通过这种方式,您可以将背景图像设置为图片或带有圆边的颜色,用于 LinearLayout。这是我的例子:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-02-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-11-04
          相关资源
          最近更新 更多