Android 布局的边框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11305382/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:50:33  来源:igfitidea点击:

Border for the layout

androidborderandroid-linearlayout

提问by MBMJ

I am trying to make a border around my layout like this

我正在尝试像这样在我的布局周围制作边框

two text areas with rounded borders

两个带有圆形边框的文本区域

But what i am trying just change the layout color. Not make any border.

但我正在尝试改变布局颜色。不做任何边框。

My code is

我的代码是

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

Why it does not work out?

为什么它不起作用?

回答by ρяσ?ρ?я K

try this:

尝试这个:

STEP 1 :Create the file layout_border.xmlin your project's drawables directory (res/drawable/layout_border.xml) :

第 1layout_border.xml步:在项目的 drawables 目录 (res/drawable/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>

STEP 2:

第2步:

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

回答by Erol

Create a file "res/drawable/my_border.xml" and define a shape:

创建一个文件“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>

Then add this as a background inside your layout tag:

然后将其添加为布局标签中的背景:

android:background="@drawable/my_border"

回答by K_Anas

Solution 1:

解决方案1:

my_edittext_bg

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>

Now set this as background of your edit Text

现在将其设置为编辑文本的背景

 android:background="@drawable/my_edittext_bg"

Solution 2:

解决方案2:

Use this 9-patch drawable as background of your EditText

使用这个 9-patch drawable 作为 EditText 的背景

enter image description here

在此处输入图片说明

回答by Krishna Suthar

Try this code:

试试这个代码:

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

And create xml file in drawable folder named 'textboxes' and write this codein that:

并在名为“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>

Fro rounder corner border, write this:

从圆角边框,写这个:

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