简体   繁体   中英

How to set textview width for different screen size width in Android?

<TextView
            android:id="@+id/text"
            android:layout_width="@dimen/width_chat"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/left"
            android:fontFamily="calibri"
            android:padding="8dp"
            android:textColor="#636363"
            android:background="@anim/mercy"
            android:textSize="17dp" />

This is my text view. I have screen size 4 and 6 inch. I want to set width so that according to that it should auto adjust. I have created 2 dimension files in values and values-sw600dp . In both folder I have set dimension width 250dp and and 150 respectively but unable to set size according to different files. Please help me and suggest me where am doing wrong.

Use a Horizontal LinearLayout and android:layout_weight property to each child.

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <View
        android:id="@+id/left"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1" >
    </View>

    <TextView
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.5"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>

If you want to use dimensions files(eg to set text_size ), enter the values in dimens.xml in the appropriate folder.

res/values/dimens.xml
res/values-small/dimens.xml
res/values-normal/dimens.xml
res/values-large/dimens.xml
res/values-xlarge/dimens.xml

您也可以简单地尝试以“ px”而不是“ dp”定义尺寸。但是,分配权重始终是一个更好的解决方案。

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