gpt4 book ai didi

android - TextView 背景正在拉伸(stretch)

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:41:23 27 4
gpt4 key购买 nike

我想将背景设置为 TextView 并且我希望 TextView 的大小与背景和文本的大小相同。

这是我的代码:

    <TextView 
android:id="@+id/attackeNameTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="13dp"
android:paddingTop="8dp"
android:paddingLeft="35dp"
android:paddingRight="35dp"
android:singleLine="true"
android:ellipsize="end"
android:textSize="12sp"
android:text="thej hhekj hejk hjkesd ks dkl jsalkj dkl"
android:background="@drawable/attackee_name_background"/>

结果是,文本正好适合“填充框”,但背景被拉伸(stretch)了。

我是否必须为 TextView 分配一个固定的 layout_width

最佳答案

如果您希望背景完全适合您的 TextView 大小而不拉伸(stretch)并且文本适合内部,您必须使用 9-patch 图像或者您必须设置固定大小等于图像尺寸。没有其他解决方案。

还请考虑 padding 仅缩小文本框区域(背景未缩小),而 marginTextView 框外留出空间.

因此,如果背景图像是 (image_width, image_height) 并且您想在 TextView 中保留一个填充,而不拉伸(stretch)背景并且不使用 9 -patch 图像,你必须将固定大小设置为 TextView 等于 (image_width+paddingLeft+paddingRight, image_height+paddingTop+paddingBottom)

关于android - TextView 背景正在拉伸(stretch),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9110122/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com