如何在 Android LinearLayout 周围创建边框?

新手上路,请多包涵

我有一个大布局,里面有一个较小的布局。

如何在小布局周围创建线条边框?

原文由 user1302569 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1k
2 个回答

当然。您可以为所需的任何布局添加边框。基本上,您需要创建一个自定义可绘制对象并将其作为背景添加到您的布局中。例子:

在您的可绘制文件夹中创建一个名为 customborder.xml 的文件:

 <?xml version="1.0" encoding="UTF-8"?>
 <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
   <corners android:radius="20dp"/>
   <padding android:left="10dp" android:right="10dp" android:top="10dp" android:bottom="10dp"/>
   <stroke android:width="1dp" android:color="#CCCCCC"/>
 </shape>

现在将其作为背景应用到较小的布局中:

 <LinearLayout android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/customborder">

这应该够了吧。

另见:

原文由 Anup Cowkur 发布,翻译遵循 CC BY-SA 3.0 许可协议

似乎没有人使用我们提出的方法,所以我想我会分享,因为它涉及更简单的代码。只需使用填充将一个布局嵌套在另一个布局中,外部的布局可以着色,形成一个框架。

         <androidx.appcompat.widget.LinearLayoutCompat
        android:id="@+id/llSignatureBorder"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:background="@android:color/black"
        android:orientation="vertical"
        android:padding="4dp">

        <com.yourmom.mobiledriverapp.Controls.SignatureArea
            android:id="@+id/signatureArea"
            android:layout_width="@dimen/signaturearea_width"
            android:layout_height="@dimen/signaturearea_height"
            android:layout_gravity="center_horizontal"
            android:layout_margin="10dp" />

    </androidx.appcompat.widget.LinearLayoutCompat>

原文由 John Lord 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题