在Android中与标题边界?

时间:2013-02-25 05:01:24

标签: android border shapes

我们可以使用shape创建一个简单的边框,例如我看到了这段代码here

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <corners android:radius="5dip" />
            <solid android:color="@color/black" />
            <stroke android:width="2dip" android:color="@color/white" />
        </shape>
    </item>
</layer-list>

但是我需要一个带有这个图像标题的边框:

enter image description here

我怎么能这样做?

2 个答案:

答案 0 :(得分:0)

尝试以下代码并在应用程序的res/drawable文件夹中创建xml文件并粘贴以下代码:

 <?xml version="1.0" encoding="utf-8"?>
  <shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >
<corners android:radius="3dp" />
<solid android:color="#00000000" />
<stroke
    android:width="2dp"
    android:color="#1B455F" />
  <padding android:left="5dp"
    android:top="5dp"
    android:right="5dp"
    android:bottom="5dp"/>
</shape>

您可以通过更改笔触颜色来根据自己的方便更改边框颜色。

使用上面的xml如下:这里我试图在TextView上设置边框。

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/border"
    android:text="@string/hello_world" />

我希望它会对你有所帮助。

感谢。

答案 1 :(得分:0)

现在我找到了一个很好的代码来帮助我here。我在这里添加代码:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" 
android:color="#000000">
<View
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/rectangle"
    android:layout_margin="20dp"
    />
<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:background="#000000"
        android:layout_marginTop="10dp"
        android:text="TITLE!"
        android:textColor="#ffffff"/>
</RelativeLayout>

@ drawable / rectangle在一个drawable rectangle.xml文件中,如下所示:

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