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

前端 未结 7 418
走了就别回头了
走了就别回头了 2021-02-03 19:27

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

相关标签:
7条回答
  • 2021-02-03 19:46

    What Jave said is correct - and the easiest, but what if you're not using a RelativeLayout to contain the View's ?

    If you're customizing your UI within onCreate() then you'll find that obtaining the width from another widget will give you an incorrect result ! That's because the UI hasn't been set up yet.

    But you can still set up your UI within onCreate... simply run code that executes after the UI is set up. This is achieved through use of the View.post() command.

    The XML :

    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:gravity="center">
    
            <Button
                android:id="@+id/button_one"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginBottom="10dp"
                android:text="short string" />
    
            <Button
                android:id="@+id/button_two"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="this is a longer string" />
    
        </LinearLayout>
    
    </RelativeLayout>
    

    Java code:

    private Button mButtonOne;
    private Button mButtonTwo;
    
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
    
        // inflate UI
        setContentView(R.layout.activity_example);
    
        // get references to UI elements
        mButtonOne = (Button)findViewById(R.id.button_one);
        mButtonTwo = (Button)findViewById(R.id.button_two);
    
        // Make buttons the same size (i.e. Button1.width = Button2.width)
        if ((mButtonOne != null) && (mButtonTwo != null))
        {
            mButtonOne.post(new Runnable()
            {
                @Override
                public void run()
                {
                    mButtonOne.setWidth(mButtonTwo.getWidth());
                }
            });
        }
    }
    

    The result is that the Width of button_one will match the Width of button_two. This is a nicer look when the amount of text varies heavily between the two View's.

    0 讨论(0)
  • 2021-02-03 19:46

    use following width attribute.

     <TextView
            android:id="@+id/textView1"
            android:layout_width="150dp"
            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="150dp"
                android:layout_height="0.3dp"
                android:layout_below="@+id/textView1"
                android:background="#ffffff" />
    
    0 讨论(0)
  • 2021-02-03 19:47

    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" />
    
    0 讨论(0)
  • 2021-02-03 19:47

    It depends on your use case. If you plan to modify things in the layout dynamically and have them also sized the same as the TextView, you may want to wrap them in a parent view together. If it's a one-time thing, use RelativeLayout as suggested by the other answer here.

    <LinearLayout android:orientation="vertical" ... > <!-- layout parameters as appropriate-->
    <TextView
            android:id="@+id/textView1"
            android:layout_width="match_parent"
            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="match_parent"
                android:layout_height="0.3dp"
                android:layout_below="@+id/textView1"
                android:background="#ffffff" />
    
     </LinearLayout>
    
    0 讨论(0)
  • 2021-02-03 19:59

    If you're using a Layout other than a RelativeLayout, you can match the widths of your widgets programmatically, such as in this example:

    layout.xml:

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        >
    
        <TextView
            android:id="@+id/text1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Here's some text"
            />
    
        <TextView
            android:id="@+id/text2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Some more text"
            />
    </LinearLayout>
    

    Notice that both text fields are both set to wrap_content.

    Main.java:

    TextView tv1 = (TextView) findViewById(R.id.text1);
    TextView tv2 = (TextView) findViewById(R.id.text2);
    
    if(tv1.getWidth() < tv2.getWidth())
        tv1.setWidth(tv2.getWidth());
    else
        tv2.setWidth(tv1.getWidth());
    

    If you have multiple widgets that you want to have a uniform width, just repeat the above code for the new element. For example, let's say there was a button I wanted to adjust the width to, to match the other elements:

    if(tv2.getWidth() < button)
        tv2.setWidth(button.getWidth());
    else
        button.setWidth(tv2.getWidth());
    
    0 讨论(0)
  • 2021-02-03 19:59

    Use RelativeLayout and use these two attribute in horizontal line view

     android:layout_alignLeft="@+id/textView1"     android:layout_alignRight="@+id/textView1"
    
    
    
    
    <RelativeLayout
            android:id="@+id/relativeLayout1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1.23" >
    
            <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="67dp"
                android:text="this is TextView" />
    
    
            <TextView
                android:id="@+id/textView2"
                android:layout_width="wrap_content"
                android:layout_height="10dp"
                android:layout_alignLeft="@+id/textView1"
                android:layout_alignRight="@+id/textView1"
                android:layout_below="@+id/textView1"
                android:background="#FF0000"
                android:text="" />
    
        </RelativeLayout>
    
    0 讨论(0)
提交回复
热议问题