简体   繁体   中英

Get TextView/Layout width according to device.

I am making an application where first i need to get the TextView size and then apply it to a Layout by adjusting it according to the TextView . But the sizes changes device to device...is there any procedure where i could cope up these differences.

If you want your Item to be the same from device to device I would place your textview inbetween to empty views in a linearlayout than use the sum-weight and adjust the layout weight to be what you want it to be. and than adjust the layout_height or width (depends on horizontal vs vertical linear layout) to the appropriate amount. for example a horizontal linear layout you would set layout_width to 0dp, vertical you would set layout_height to 0dp. here is some example code from an old project

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:weightSum="4" >

        <View
        android:id="@+id/view4"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="1" />

        <Button
        android:id="@+id/add_new_customer_button"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="2"
        android:text="@string/add_customer_button_label"/>

        <View
        android:id="@+id/view5"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="1" />

</LinearLayout>

this would make something that looks like

[empty view][Button THAT IS 1/2 of SCREEN CENTERED][empty view]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM