Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
353 views
in Technique[技术] by (71.8m points)

legend and fieldset in android

I want to know if it's possible in android to do a form like that :

enter image description here

At this moment, I made a shape to produce the border (I use this shape as a background of my linearlayout, but i don't know how to deal with the text).

Thanks in advance.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I have managed to get a frame with title using the following technique.

  1. Use FrameLayout for the overall layout.

  2. Inside add another layout for your main content - this can be anything. For the background of this layout, use a custom XML drawable with stroke to draw the frame. Make sure to add margins in this layout, especially marginTop, as you need some room to display the title of the frame. Also add some padding to make it look nicer.

  3. Use TextView with opaque background and some paddings as the title. Set marginLeft on it to a reasonable value to offset the title from the left.

Here's my XML for this:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    android:background="@android:color/white">

    <!--  This is the main content -->
    <RelativeLayout
        android:layout_width="fill_parent" android:layout_height="fill_parent"
        android:layout_margin="15dp" android:background="@drawable/frame"
        android:orientation="vertical" android:padding="20dp">

        <TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:text="Main Content" android:layout_centerInParent="true" />

    </RelativeLayout>

    <!--  This is the title label -->
    <TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:background="@android:color/white" android:padding="5dp"
        android:text="Testing"
        android:layout_marginLeft="30dp" android:textColor="@android:color/black" />

</RelativeLayout>

When executed, I get this on the app:

enter image description here

Feel free to tweak the margins/paddings to align the title the way you need.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...