本文介绍了有没有更简单/更好的方式来把边界/轮廓在我的TextView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我想和它周围的很酷的边界一个TextView。我找不到这样做的任何标准的办法,所以我想出了这一点:

So I wanted to have a TextView with a cool border around it. I couldn't find any standard way of doing it, so I came up with this:

@绘制/ custom_bg_1:一个蓝色的圆形

@drawable/custom_bg_1: A blue rounded shape

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#439CC8"/>
    <corners android:radius="3dp" />
    <padding android:left="10dp" android:top="10dp"
        android:right="10dp" android:bottom="10dp" />
</shape>

@绘制/ custom_bg_2:白色圆润的造型

@drawable/custom_bg_2: A white rounded shape

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

myactivity.xml:将XML的活动

myactivity.xml: the xml for the activity

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="vertical"
  android:padding="15dp" >

  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="2dp"
    android:background="@drawable/custom_bg_1" >

      <TextView android:layout_width="fill_parent"
            android:layout_height="wrap_content"
        android:text="@string/curr_loc"
        android:textSize="15sp"
        android:background="@drawable/custom_bg_2" />

  </LinearLayout>
</Linearlayout>

结果:

我在做什么这里重叠的白色形状背景蓝色形状背景里给一个蓝色边框的效果。我无法想象这是获得这种效果的最佳途径。我已经看到,试图解决这个问题,如this和this,但我觉得他们只是尽可能多的周围的工作为我实现。

What I am doing here is overlapping a white shape background inside a blue shape background to give the effect of a blue border. I can't imagine this is the best way to get this effect. I have seen other posts that attempt to solve this problem such as this and this, but I feel like they are just as much of a work around as my implementation.

有没有更好的方法或干脆把周围的某些视图的边框,如一个TextView或者我应该只是坚持我做的方式更标准的方式?

Is there a better way or more standard way of simply putting a border around certain views such as a TextView or should I just stick with the way I am doing it?

修改

我改变custom_bg_2.xml看起来是这样的:

I changed custom_bg_2.xml to look like this:

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

而现在我得到这样的结果:

And now I get this result:

它看起来像我可以通过包括实现提纲&LT;行程... /&GT; 形状

It looks like I can achieve an outline by including <stroke ... /> in shape.

推荐答案

您也许可以通过只custom_bg_2如果添加了一个&LT;行程>部分:

You can probably get by with just custom_bg_2 if you add a <stroke> section:

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

这篇关于有没有更简单/更好的方式来把边界/轮廓在我的TextView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 10:09