如何围绕Android LinearLayout创建边框?

问题描述 投票:87回答:7

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

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

android android-layout android-linearlayout
7个回答
193
投票

当然。您可以为所需的任何布局添加边框。基本上,您需要创建自定义drawable并将其添加为布局的背景。例:

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

这应该够了吧。

另见:


22
投票

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

10
投票

试试这个:

例如,让我们将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>

9
投票

在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称为您的小布局背景

机器人:背景= “@绘制/ yourxml”


3
投票

此解决方案只会添加边框,LinearLayout的主体将是透明的。

首先,在drawable文件夹border.xml中创建此边框drawable

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

3
投票

我会将Android文档链接添加到其他答案中。

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>

红色边界有透明背景。


2
投票

你也可以用实际的方式去做

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

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

LinearLayout layout = (LinearLayout ) findViewById(R.id.ayout); layout .setBackground(gradientDrawable);
© www.soinside.com 2019 - 2024. All rights reserved.