布局边框

18

我想要像这样在我的布局周围加上一个边框:

两个带圆角边框的文本区域

但我尝试的只是改变布局的颜色, 并没有创建任何边框。

我的代码如下:

<?xml version="1.0" encoding="UTF-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android"
> 

<stroke android:width="1dp"

        android:color="#eedfcc"
        android:background="#000080"/>

<corners android:bottomRightRadius="20dp"
         android:bottomLeftRadius="20dp" 
         android:topLeftRadius="10dp"
         android:topRightRadius="8dp"/> 
</shape>

为什么它不起作用?


你没有像这样定义你的形状种类:<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" > - Mohammad Ersan
实际上它并不是完全的矩形。 - MBMJ
4个回答

65

尝试这个:

步骤1:在您的项目的可绘制目录(res / drawable / layout_border.xml)中创建文件layout_border.xml

<?xml version="1.0" encoding="UTF-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android"> 
    <solid android:color="#FFFFFF"/> 
    <stroke android:width="3dip" android:color="#B1BCBE" />
    <corners android:radius="10dip"/>
    <padding android:left="0dip" android:top="0dip" android:right="0dip" android:bottom="0dip" />
</shape>

步骤2:

<EditText 
    android:id="@+id/edittext"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/layout_border"
     /> 

真是一个非常好的解决方案。快速、简单且文档清晰易懂。我也能够在Android Studio 2.1.1中轻松实现它。 - raddevus

3
创建一个文件"res/drawable/my_border.xml"并定义一个形状:
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <stroke android:width="4dp" android:color="#FF00FF00" />
    <solid android:color="#ffffff" />
    <padding android:left="7dp" android:top="7dp"
            android:right="7dp" android:bottom="7dp" />
    <corners android:radius="4dp" />
</shape>

然后在您的布局标签内添加此背景:
android:background="@drawable/my_border"

2

Solution 1:

my_edittext_bg

 <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android" >    
    <!-- Color of the border -->
    <stroke
      android:width="2dp"
      android:color="#FF0000" />
    <!-- Use this if you want to round your Corners -->
    <corners android:radius="5dp" />
    <!-- Use this attribute if you want add some paddings -->
    <padding android:left="0dip" android:top="0dip" android:right="0dip" android:bottom="0dip" />
    </shape>

现在将此设置为您编辑文本的背景。
 android:background="@drawable/my_edittext_bg"

解决方案 2:

将这个9-patch图形作为EditText的背景。

enter image description here


1

尝试这段代码:

<EditText 
    android:id="@+id/edittext1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/textboxes"
     /> 

在drawable文件夹中创建名为“textboxes”的xml文件,并在其中编写以下代码:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<stroke
    android:color="#f269be"
    android:width="2dp" />
<solid
    android:color="#ffffff" />
</shape>

为了使边框拥有圆角,可以这样写:

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

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