【发布时间】:2016-09-06 04:53:09
【问题描述】:
我在 ListView 中使用 TextView 时遇到问题,其中 TextView 错误地换行。
为了说明问题,我要显示以下文本:
"12345678901234567 ="
"12345678901 ="
"12345678901234567 *"
我使用的xml(TextView)如下:-
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="end"
android:textAlignment="gravity"
android:layout_gravity="end"
android:textAppearance="?android:attr/textAppearanceLarge"
/>
结果显示如下:
123456789
01234567
=
12345678901=
123456789
01234567
*
显然我不希望“=”和“*”位于单独的行上。我有 尝试了几十种不同的 xml 变体,但没有达到我想要的效果。
测试的模拟器是:
Nexus 6 API 23:5556
Nexus6p API 23:5556
MotoG3 Android 6
使用 23.0.3 为 Android 6 编译
使用以下 xml (EditText),我确实得到了想要的结果:
<?xml version="1.0" encoding="utf-8"?>
<EditText xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="end"
android:textAlignment="gravity"
android:layout_gravity="end"
android:textAppearance="?android:attr/textAppearanceLarge"
android:editable="false">
</EditText>
正确结果如下:
12345678901234
567 =
12345678901 =
12345678901234
567 *
虽然我可以使用 EditText,但显示不应该是可编辑的 而且我相信 TextView 应该可以工作。此外,“机器人:可编辑” 已弃用。
有人可以告诉我如何获取 TextView 根据需要工作,或者如何使用 EditText 一个未弃用的功能并达到预期的结果?
.........添加到问题---------
在test prog中用于创建ListView的代码如下:-
private void createLayout() {
RelativeLayout wRelLayout = new RelativeLayout(this);
wRelLayout.setBackgroundColor(Color.DKGRAY);
RelativeLayout.LayoutParams jLayParams = new RelativeLayout.LayoutParams(500, 700);
setContentView(wRelLayout, jLayParams);
ArrayList<String> alCalcLines = new ArrayList();
ListView lvCalcLines = new ListView(this);
lvCalcLines.setX(10);
lvCalcLines.setY(10);
lvCalcLines.setBackgroundColor(Color.WHITE);
lvCalcLines.setPadding(10, 10, 10, 10);
lvCalcLines.setAdapter(new ArrayAdapter<>(
this, R.layout.text_view_1, alCalcLines)); // xml
jLayParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT);
wRelLayout.addView(lvCalcLines, jLayParams);
alCalcLines.add("12345678901234567 =");
alCalcLines.add("12345678901 =");
alCalcLines.add("12345678901234567 *");
}
【问题讨论】:
标签: android xml android-layout