如何在Android LinearLayout中创建边框?

145
我有一个大布局,里面有一个较小的布局。
如何在小布局周围创建一条线框?

你尝试过https://dev59.com/SnA75IYBdhLWcg3wDUm2吗? - sandrstar
10个回答

313
当然可以。您可以为任何布局添加边框。基本上,您需要创建一个自定义的可绘制对象,并将其添加为布局的背景。例如:
在drawable文件夹中创建一个名为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">

那应该就可以了。
另外请参考:

32
将会在矩形内填充颜色。您需要使用 <stroke> 命令绘制边框而非填充。 - NightFury
2
这里的诀窍实际上是<填充>,花了我一段时间才发现为什么我的形状不起作用。有了填充就可以正常工作了。 - John
3
<solid/><stroke/>都填满整个矩形吗?这是我的代码有bug吗? - mr5
13
添加 <stroke android:width="1dip" android:color="#CCCCCC" />,它的效果很好。 - Pierry
1
Pierry的评论使得这个答案正确,否则你只能得到一个填满的背景。 - kagkar
显示剩余2条评论

27
在drawable文件夹中创建名为border.xml的XML文件,内容如下:
<?xml version="1.0" encoding="utf-8"?>
 <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
  <item> 
    <shape android:shape="rectangle">
      <solid android:color="#FF0000" /> 
    </shape>
  </item>   
    <item android:left="5dp" android:right="5dp"  android:top="5dp" >  
     <shape android:shape="rectangle"> 
      <solid android:color="#000000" />
    </shape>
   </item>    
 </layer-list>

然后将这个作为背景添加到线性布局中:

     android:background="@drawable/border"

11

请尝试以下操作:

例如,我们定义 res/drawable/my_custom_background.xml 如下:

(在 drawable 文件夹中创建此布局) layout_border.xml

  <?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <stroke android:width="2dp" android:height="2dp"
                android:color="#FF0000" />
            <solid android:color="#000000" />
            <padding android:left="1dp" android:top="1dp" android:right="1dp"
                android:bottom="1dp" />

            <corners android:radius="1dp" android:bottomRightRadius="5dp"
                android:bottomLeftRadius="0dp" android:topLeftRadius="5dp"
                android:topRightRadius="0dp" />
        </shape>
    </item>

</layer-list>

main.xml

<LinearLayout 
    android:layout_gravity="center"
    android:layout_width="200dp" 
    android:layout_height="200dp"   
    android:background="@drawable/layout_border" />
</LinearLayout>

11
在drawable文件夹中创建一个xml文件。

<stroke
    android:width="2dp"
    android:color="#B40404" />

<padding
    android:bottom="5dp"
    android:left="5dp"
    android:right="5dp"
    android:top="5dp" />

<corners android:radius="4dp" />

现在将这个xml调用到你的小布局背景中:
android:background="@drawable/yourxml"

此处不允许使用元素描边。 - dot_zero

5
这个解决方案只会增加边框,LinearLayout的主体将保持透明。首先,在drawable文件夹中创建边框可绘制项,名为border.xml
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android= "http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <stroke android:width="2dp" android:color="#ec0606"/>
    <corners android:radius="10dp"/>
</shape>

然后,在你的LinearLayout视图中,像这样将border.xml作为背景添加。
<LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@drawable/border">

4

我会在其他答案中添加安卓文档链接。

https://developer.android.com/guide/topics/resources/drawable-resource.html#Shape

它描述了Shape Drawable的所有属性,其中包括stroke以设置边框。

示例:

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
  <stroke android:width="1dp" android:color="#F00"/>
  <solid android:color="#0000"/>
</shape>

红色边框,透明背景。

实心部分允许仅对边框设置色调。+1 - aksh1618

4

你也可以通过实际操作来完成它

  GradientDrawable gradientDrawable=new GradientDrawable();
   gradientDrawable.setStroke(4,getResources().getColor(R.color.line_Input));

然后将布局的背景设置为:

LinearLayout layout = (LinearLayout ) findViewById(R.id.ayout); layout .setBackground(gradientDrawable);

1
不想创建可绘制资源?
  <FrameLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/black"
    android:minHeight="128dp">

    <FrameLayout
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:layout_margin="1dp"
      android:background="@android:color/white">

      <TextView ... />
    </FrameLayout>
  </FrameLayout>

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>

0

尝试将此放入您的res/drawable中

    <?xml version="1.0" encoding="UTF-8"?><layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
  <shape
    android:shape="rectangle">
    <padding android:left="15dp"
        android:right="15dp"
        android:top="15dp"
        android:bottom="15dp"/>
    <stroke android:width="10dp"
        android:color="@color/colorPrimary"/>
  </shape>
</item><item android:left="-5dp"
        android:right="-5dp"
        android:top="-5dp"
        android:bottom="-5dp">
  <shape android:shape="rectangle">
    <solid android:color="@color/background" />
  </shape></item></layer-list>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接