![](/img/trans.png)
[英]Android: How do I set max-width of TextVIew as ratio of screen size?
[英]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" />
这是我的文本视图。 我的屏幕尺寸为4和6英寸。 我想设置宽度,以便根据它应该自动调整。 我已经在values
和values-sw600dp
创建了2个维度文件。 在两个文件夹中,我分别设置了尺寸宽度250dp和150,但是无法根据不同的文件设置尺寸。 请帮助我,并建议我哪里做错了。
对每个孩子使用Horizontal LinearLayout
和android:layout_weight
属性。
<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>
如果要使用尺寸文件(例如,设置text_size
), dimens.xml
在适当的文件夹中的dimens.xml
中输入值。
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”定义尺寸。但是,分配权重始终是一个更好的解决方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.