Kon*_*rov 1214

您可以将形状可绘制(矩形)设置为视图的背景.

<TextView android:text="Some text" android:background="@drawable/back"/>
Run Code Online (Sandbox Code Playgroud)

和矩形drawable back.xml(放入res/drawable文件夹):

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
   <solid android:color="@android:color/white" />
   <stroke android:width="1dip" android:color="#4fa5d5"/>
</shape>
Run Code Online (Sandbox Code Playgroud)

您可以使用@android:color/transparent纯色来获得透明背景.您还可以使用填充来将文本与边框分开.有关详细信息,请参阅:http://developer.android.com/guide/topics/resources/drawable-resource.html

  • 如果我只想要顶部边框怎么办? (82认同)
  • @whyoz他的方法为视图层次结构增加了不必要的复杂性.您将需要两个额外的视图(布局容器和边框视图)来使用他的方法.因此,如果您需要添加边框的视图很多,那么您的视图树将变得无法管理. (19认同)
  • 仅限**上边框**,请参阅[此问题](http://stackoverflow.com/q/1598119/1402846). (11认同)
  • 您可以使用此工具[http://shapes.softartstudio.com](http://shapes.softartstudio.com/)生成drawable. (6认同)
  • @whyoz但是这个方法可能通过样式和主题应用,而YongGu的方法不能以这种方式使用. (5认同)
  • 我不敢相信如今在 Android 布局上做如此平庸的事情仍然如此困难 (2认同)

Sur*_*gch 171

让我总结一些不同的(非程序化)方法.

使用可绘制的形状

将以下内容保存为drawable文件夹中的XML文件(例如,my_border.xml):

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

    <!-- View background color -->
    <solid
        android:color="@color/background_color" >
    </solid>

    <!-- View border color and width -->
    <stroke
        android:width="1dp"
        android:color="@color/border_color" >
    </stroke>

    <!-- The radius makes the corners rounded -->
    <corners
        android:radius="2dp"   >
    </corners>

</shape>
Run Code Online (Sandbox Code Playgroud)

然后将其设置为TextView的背景:

<TextView
    android:id="@+id/textview1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/my_border" />
Run Code Online (Sandbox Code Playgroud)

更多帮助:

使用9补丁

9补丁是可伸缩的背景图像.如果您使用边框制作图像,则会为TextView提供边框.您需要做的就是制作图像,然后在TextView中将其设置为背景.

<TextView
    android:id="@+id/textview1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/my_ninepatch_image" />
Run Code Online (Sandbox Code Playgroud)

以下是一些链接,将显示如何制作9补丁图像:

如果我只想要顶部边框怎么办?

使用图层列表

您可以使用图层列表将两个矩形堆叠在一起.通过使第二个矩形比第一个矩形略小,您可以创建边框效果.第一个(下部)矩形是边框颜色,第二个矩形是背景颜色.

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

    <!-- Lower rectangle (border color) -->
    <item>
        <shape android:shape="rectangle">
            <solid android:color="@color/border_color" />
        </shape>
    </item>

    <!-- Upper rectangle (background color) -->
    <item android:top="2dp">
        <shape android:shape="rectangle">
            <solid android:color="@color/background_color" />
        </shape>
    </item>
</layer-list>
Run Code Online (Sandbox Code Playgroud)

设置android:top="2dp"顶部偏移(使其变小)2dp.这允许第一个(下部)矩形显示,给出边框效果.您可以像shape上面的drawable 一样将它应用于TextView背景.

以下是有关图层列表的更多链接:

使用9补丁

您可以使用单个边框制作9补丁图像.其他一切与上面讨论的相同.

使用视图

这是一种技巧,但如果您需要在两个视图之间添加分隔符或者将边框添加到单个TextView,它会很有效.

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textview1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <!-- This adds a border between the TextViews -->
    <View
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:background="@android:color/black" />

    <TextView
        android:id="@+id/textview2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

这里有一些更多的链接:

  • Jeez man,一个`border:1px solid#999;`不应该是*这个*复杂. (17认同)

小智 48

简单的方法是为TextView添加一个视图.底部边框线的示例:

<LinearLayout android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent">
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="10dp"
        android:text="@string/title"
        android:id="@+id/title_label"
        android:gravity="center_vertical"/>
    <View
        android:layout_width="fill_parent"
        android:layout_height="0.2dp"
        android:id="@+id/separator"
        android:visibility="visible"
        android:background="@android:color/darker_gray"/>

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

对于其他方向边框,请调整分隔符视图的位置.

  • 从通胀观点来看,这是一个可怕的解决方案.您可以创建3个新视图元素,并在视图层次结构深度中再添加一个级别 (41认同)
  • 根据我的经验,此解决方案在旧设备上对应用程序性能有明显的明显影响. (3认同)

Boj*_*vic 31

我通过扩展textview并手动绘制边框解决了这个问题.我甚至添加了这样你可以选择边框是虚线还是虚线.

public class BorderedTextView extends TextView {
        private Paint paint = new Paint();
        public static final int BORDER_TOP = 0x00000001;
        public static final int BORDER_RIGHT = 0x00000002;
        public static final int BORDER_BOTTOM = 0x00000004;
        public static final int BORDER_LEFT = 0x00000008;

        private Border[] borders;

        public BorderedTextView(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
            init();
        }

        public BorderedTextView(Context context, AttributeSet attrs) {
            super(context, attrs);
            init();
        }

        public BorderedTextView(Context context) {
            super(context);
            init();
        }
        private void init(){
            paint.setStyle(Paint.Style.STROKE);
            paint.setColor(Color.BLACK);
            paint.setStrokeWidth(4);        
        }
        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            if(borders == null) return;

            for(Border border : borders){
                paint.setColor(border.getColor());
                paint.setStrokeWidth(border.getWidth());

                if(border.getStyle() == BORDER_TOP){
                    canvas.drawLine(0, 0, getWidth(), 0, paint);                
                } else
                if(border.getStyle() == BORDER_RIGHT){
                    canvas.drawLine(getWidth(), 0, getWidth(), getHeight(), paint);
                } else
                if(border.getStyle() == BORDER_BOTTOM){
                    canvas.drawLine(0, getHeight(), getWidth(), getHeight(), paint);
                } else
                if(border.getStyle() == BORDER_LEFT){
                    canvas.drawLine(0, 0, 0, getHeight(), paint);
                }
            }
        }

        public Border[] getBorders() {
            return borders;
        }

        public void setBorders(Border[] borders) {
            this.borders = borders;
        }
}
Run Code Online (Sandbox Code Playgroud)

边境班:

public class Border {
    private int orientation;
    private int width;
    private int color = Color.BLACK;
    private int style;
    public int getWidth() {
        return width;
    }
    public void setWidth(int width) {
        this.width = width;
    }
    public int getColor() {
        return color;
    }
    public void setColor(int color) {
        this.color = color;
    }
    public int getStyle() {
        return style;
    }
    public void setStyle(int style) {
        this.style = style;
    }
    public int getOrientation() {
        return orientation;
    }
    public void setOrientation(int orientation) {
        this.orientation = orientation;
    }
    public Border(int Style) {
        this.style = Style;
    }
}
Run Code Online (Sandbox Code Playgroud)

希望这有助于某人:)


Gab*_*tti 16

通过材料组件库,您可以使用MaterialShapeDrawable.

    <TextView
        android:id="@+id/textview"
        .../>
Run Code Online (Sandbox Code Playgroud)

然后您可以以编程方式应用MaterialShapeDrawable

    TextView textView = findViewById(R.id.textview);
    MaterialShapeDrawable shapeDrawable = new MaterialShapeDrawable();
    shapeDrawable.setFillColor(ContextCompat.getColorStateList(this,android.R.color.transparent));
    shapeDrawable.setStroke(1.0f, ContextCompat.getColor(this,R.color....));
    ViewCompat.setBackground(textView,shapeDrawable);
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述


小智 14

我只是在看一个类似的答案 - 它可以通过一个笔画和以下覆盖来完成:

@Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) {

Paint strokePaint = new Paint();
strokePaint.setARGB(255, 0, 0, 0);
strokePaint.setTextAlign(Paint.Align.CENTER);
strokePaint.setTextSize(16);
strokePaint.setTypeface(Typeface.DEFAULT_BOLD);
strokePaint.setStyle(Paint.Style.STROKE);
strokePaint.setStrokeWidth(2);

Paint textPaint = new Paint();
textPaint.setARGB(255, 255, 255, 255);
textPaint.setTextAlign(Paint.Align.CENTER);
textPaint.setTextSize(16);
textPaint.setTypeface(Typeface.DEFAULT_BOLD);

canvas.drawText("Some Text", 100, 100, strokePaint);
canvas.drawText("Some Text", 100, 100, textPaint);

super.draw(canvas, mapView, shadow); 
}
Run Code Online (Sandbox Code Playgroud)


Mau*_*oki 13

您可以通过两种方法设置边框.一个是可绘制的,第二个是程序化的.

使用Drawable

<shape>
    <solid android:color="@color/txt_white"/>
    <stroke android:width="1dip" android:color="@color/border_gray"/>
    <corners android:bottomLeftRadius="10dp"
             android:bottomRightRadius="0dp"
             android:topLeftRadius="10dp"
             android:topRightRadius="0dp"/>
    <padding android:bottom="0dip"
             android:left="0dip"
             android:right="0dip"
             android:top="0dip"/>
</shape>
Run Code Online (Sandbox Code Playgroud)

编程


public static GradientDrawable backgroundWithoutBorder(int color) {

    GradientDrawable gdDefault = new GradientDrawable();
    gdDefault.setColor(color);
    gdDefault.setCornerRadii(new float[] { radius, radius, 0, 0, 0, 0,
                                           radius, radius });
    return gdDefault;
}
Run Code Online (Sandbox Code Playgroud)

  • 该形状 XML 与 textView 有何关联? (3认同)

Pro*_*ean 13

我找到的最简单的解决方案(实际上有效):

<TextView
    ...
    android:background="@android:drawable/editbox_background" />
Run Code Online (Sandbox Code Playgroud)

  • 很适合快速行动。但根据运行设备的版本,这可能会产生非常不寻常的效果。这是因为 Google 不断改变 EditText 的外观(你必须保持时尚!) (3认同)

Mar*_*hat 12

您可以将可绘制的形状(带角的矩形)设置为视图的背景。

<TextView android:background="@drawable/frame"/>
Run Code Online (Sandbox Code Playgroud)

和矩形可绘制frame.xml(放入res/drawable文件夹中):

<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >
    <solid android:color="@android:color/white" />
    <stroke android:width="1dip"
     android:color="#3d4caf"/>
    <corners android:radius="50dp"/>
</shape>
Run Code Online (Sandbox Code Playgroud)


Nic*_*ick 11

我找到了一种更好的方法来在TextView周围放置边框.

使用九个补丁图像作为背景.这很简单,SDK附带了一个制作9补丁图像的工具,它绝对不需要编码.

该链接是http://developer.android.com/guide/topics/graphics/2d-graphics.html#nine-patch.

  • 有用,是的,但这怎么样更好? (2认同)
  • 使用形状作为接受的答案说比9补丁更好,XML文件比图形资产更灵活 (2认同)
  • 形状方法是无用的.也许我是唯一经历过的,标签"solid"总是被处理,无论是否设置为透明.如果我想创建一个BORDER,我的意思是,一个BORDER而不是带有彩色内边和其他彩色边框的矩形.它永远不透明.它总是会扰乱颜色.另外,一个继承自另一个类的类也应该具有所有功能,因为它是继承的.真的无法理解,为什么基本的OO设计指南被android破坏了.例如,我从按钮继承.所有功能都没了.为什么? (2认同)
  • @Jeremie明显慢了吗?你是如何观察它的? (2认同)

小智 11

您可以在代码中添加以下内容:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="rectangle" >
    <solid android:color="#ffffff" />
    <stroke android:width="1dip" android:color="#4fa5d5"/>
</shape>
Run Code Online (Sandbox Code Playgroud)


Bah*_*any 10

检查下面的链接以制作圆角 http://androidcookbook.com/Recipe.seam?recipeId=2318

Android项目中res下的drawable文件夹不限于位图(PNG或JPG文件),但它也可以保存XML文件中定义的形状.

然后可以在项目中重用这些形状.形状可用于在布局周围放置边框.此示例显示了带有弯角的矩形边框.在drawable文件夹中创建一个名为customborder.xml的新文件(在Eclipse中使用File菜单并选择New then File,在文件名中选择drawable文件夹,然后单击Finish).

输入定义边框形状的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"/>
    <solid android:color="#CCCCCC"/>
</shape>
Run Code Online (Sandbox Code Playgroud)

该属性android:shape设置为矩形(形状文件也支持椭圆,直线和圆环).Rectangle是默认值,因此如果它是一个定义的矩形,则可以省略该属性.有关形状文件的详细信息,请参阅http://developer.android.com/guide/topics/resources/drawable-resource.html#Shape上有关形状的Android文档.

元素角将矩形角设置为圆角.可以在每个角上设置不同的半径(请参阅Android参考).

填充属性用于移动应用了形状的视图的内容,以防止内容与边框重叠.

此处的边框颜色设置为浅灰色(CCCCCC十六进制RGB值).

形状也支持渐变,但这里没有使用.再次,请参阅Android资源以查看如何定义渐变.使用将形状应用于套管android:background="@drawable/customborder".

在布局中,可以正常添加其他视图.在此示例中,添加了单个TextView,文本为白色(FFFFFF十六进制RGB).背景设置为蓝色,加上一些透明度以降低亮度(A00000FF十六进制alpha RGB值).最后,布局通过将其放入具有少量填充的另一布局来偏离屏幕边缘.因此,完整的布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:padding="5dp">
    <LinearLayout android:orientation="vertical"
                  android:layout_width="fill_parent"
                  android:layout_height="fill_parent"
                  android:background="@drawable/customborder">
        <TextView android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:text="Text View"
                android:textSize="20dp"
                android:textColor="#FFFFFF"
                android:gravity="center_horizontal"
                android:background="#A00000FF" />
    </LinearLayout>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)


Rom*_*get 7

我有办法非常简单地完成它,我想分享它.

当我想对mi TextViews进行平方时,我只是将它们放在LinearLayout中.我设置了LinearLayout的背景颜色,并为TextView添加了一个边距.结果就像你对TextView进行平方.


小智 5

You can create custom background for your text view. Steps

  1. Go to your project.
  2. Go to resources and right click to drawable.
  3. Click on New -> Drawable Resource File
  4. Give name to you file
  5. Paste following code in the file
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <stroke
        android:width="1dp"
        android:color="@color/colorBlack" />
    <padding
        android:bottom="1dp"
        android:left="1dp"
        android:right="1dp"
        android:top="1dp" />
    <corners android:radius="6dp" />
    <solid android:color="#ffffffff" />
</shape>
Run Code Online (Sandbox Code Playgroud)
  1. For your text view where you want to use it as backgroud,

    android:background="@drawable/your_fileName"


归档时间:

查看次数:

679817 次

最近记录:

6 年,3 月 前