如何在Android中创建带标题的边框?

6

有人有想法如何创建类似于 swing 中的有标题边框布局吗?

this 网站上,我发现了一个生成对象之间矩形的技巧。也许这可以帮助我创建这样的有标题边框?

问候

1个回答

6

简单粗暴:

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

</FrameLayout>

@drawable/rectangle是在一个名为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>

我知道这样做很快,但是它为什么会变得混乱呢?这不是 <View> 标签的作用吗?我想你也可以定义一个单独的标题布局,并使用 <include> 在顶部包含它。 - Jade Byfield
这肯定可以是单独的布局。 - anthropomo

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