我倾向于创建 TextView
和 EditText
,它们在视觉上看起来像素相同。
我将 EditText
与 TextView
重叠,以便我可以比较它们的相同之处。我使 TextView
具有红色文本。
这就是它们目前的样子。
EditText
和 TextView
在没有 android:lineSpacingMultiplier
的情况下看起来相同
这是正在使用的 XML
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/body_text_view"
android:gravity="top"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:layout_marginBottom="12dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent"
android:textSize="24sp"
android:singleLine="false"
android:textColor="#ff0000" />
<EditText
android:id="@+id/body_edit_text"
android:gravity="top"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:layout_marginBottom="12dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent"
android:scrollbars="vertical"
android:textSize="24sp"
android:singleLine="false"
android:inputType="textMultiLine|textCapSentences" />
</FrameLayout>
<小时/>
但是,当使用 android:lineSpacingMultiplier
时,两者的行为有所不同。
EditText
和 TextView
与 android:lineSpacingMultiplier
看起来不同
这是正在使用的 XML
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/body_text_view"
android:gravity="top"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:layout_marginBottom="12dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent"
android:textSize="24sp"
android:singleLine="false"
android:textColor="#ff0000"
android:lineSpacingMultiplier="2.4" />
<EditText
android:id="@+id/body_edit_text"
android:gravity="top"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:layout_marginBottom="12dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent"
android:scrollbars="vertical"
android:textSize="24sp"
android:singleLine="false"
android:inputType="textMultiLine|textCapSentences"
android:lineSpacingMultiplier="2.4" />
</FrameLayout>
<小时/>
我可以知道,如何使 android:lineSpacingMultiplier 对 EditText
和 TextView
表现相同,以便 EditText
和 TextView
视觉上看起来相同吗?
p/s
请不要建议使用 EditText
替代 TextView
。他们不一样!您可以使用各种解决方法来尝试使 EditText
表现得像 TextView
。 (Stackoverflow 有关于解决方法的建议。没有一个能完美工作)最终,它不起作用!
最佳答案
EditText
是 TextView
的扩展(子类),但两者的行为不同,这肯定是因为它们的属性不相似(无论是从xml 或默认情况下不相似的那些)。因此,完美对齐它们的唯一方法是寻找那些可能导致问题的不相似属性,并使它们表现得相似。
考虑到“我可以知道,如何使 android:lineSpacingMultiplier 对 EditText 和 TextView 的行为相同,以便 EditText 和 TextView 在视觉上看起来相同?” 作为您的主要关注点,
我能够通过更改 TextView
的以下属性来完美对齐它们。
As stated in this document (TextView class).
android:editable
If set, specifies that this TextView has an input method. It will be a textual one unless it has otherwise been specified. For TextView, this is false by default. For EditText, it is true by default.
此属性已被弃用,但可以替换为
android:inputType
The type of data being placed in a text field, used to help an input method decide how to let the user enter text. The constants here correspond to those defined by InputType. Generally you can select a single value, though some can be combined together as indicated. Setting this attribute to anything besides none also implies that the text is editable.
为了使其高效工作,请使用 textMultiLine
,因为您的 EditText
具有相同的属性。
android:breakStrategy
Sets the break strategy for breaking paragraphs into lines. The default value for TextView is Layout.BREAK_STRATEGY_HIGH_QUALITY, and the default value for EditText is Layout.BREAK_STRATEGY_SIMPLE, the latter to avoid the text "dancing" when being edited.
简而言之,添加
android:inputType="textMultiLine"
android:breakStrategy="simple"
到 TextView
会将它们放置在完全相同的位置。
关于java - 如何制作安卓:lineSpacingMultiplier behaves same for both EditText and TextView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54075706/