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
227 views
in Technique[技术] by (71.8m points)

how to set width which is equal to another widget on android

I need to draw a horizontal line below a text field such that the width of the line equals the text width (not the width of the full screen).

In my app I have a textview below a view(Horizontal line). The width of the line view should be equal to the width of the textview. I tried android:layout_width="wrap_content" and "match_parent", which does not solve the problem.

This is xml coding sample:

         ......
        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="28dp"
            android:text="PopUpWindow"
            android:textAppearance="?android:attr/textAppearanceLarge" />


            <View
                android:id="@+id/separator"
                android:layout_width="wrap_content"
                android:layout_height="0.3dp"
                android:layout_below="@+id/textView1"
                android:background="#ffffff" />
             ......

image of the screen is:

enter image description here

please help me.

question from:https://stackoverflow.com/questions/9977721/how-to-set-width-which-is-equal-to-another-widget-on-android

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

1 Reply

0 votes
by (71.8m points)

If you use a RelativeLayout you can use the align-attributes:

<View
    android:id="@+id/separator"
    android:layout_width="0dp"
    android:layout_height="0.3dp"
    android:layout_below="@+id/textView1"
    android:layout_alignLeft="@+id/textView1"
    android:layout_alignRight="@+id/textView1"
    android:background="#ffffff" />

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

...