How can I create a border around an Android LinearLayout?

前端 未结 9 993
长发绾君心
长发绾君心 2020-12-22 20:33

I have one big layout, and one smaller layout inside of it.

How do I create a line border around the small layout?

相关标签:
9条回答
  • 2020-12-22 21:18

    Sure. You can add a border to any layout you want. Basically, you need to create a custom drawable and add it as a background to your layout. example:

    Create a file called customborder.xml in your drawable folder:

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

    Now apply it as a background to your smaller layout:

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

    That should do the trick.

    Also see:

    • http://tekeye.biz/2012/add-a-border-to-an-android-layout
    • How to add border around linear layout except at the bottom?
    0 讨论(0)
  • 2020-12-22 21:23

    Try this:

    For example, let's define res/drawable/my_custom_background.xml as:

    (create this layout in your drawable folder) 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>
    
    0 讨论(0)
  • 2020-12-22 21:24

    I'll add Android docs link to other answers.

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

    It describes all attributes of the Shape Drawable and stroke among them to set the border.

    Example:

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

    Red border with transparent background.

    0 讨论(0)
  • 2020-12-22 21:27

    This solution will only add the border, the body of the LinearLayout will be transparent.

    First, Create this border drawable in the drawable folder, 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>
    

    Then, in your LinearLayout View, add the border.xml as the background like this

    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:background="@drawable/border">
    
    0 讨论(0)
  • 2020-12-22 21:30

    Don't want to create a drawable resource?

      <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 讨论(0)
  • 2020-12-22 21:31

    Create a one xml file in drawable folder

    <stroke
        android:width="2dp"
        android:color="#B40404" />
    
    <padding
        android:bottom="5dp"
        android:left="5dp"
        android:right="5dp"
        android:top="5dp" />
    
    <corners android:radius="4dp" />
    

    Now call this xml to your small layout background

    android:background="@drawable/yourxml"

    0 讨论(0)
提交回复
热议问题