Android 如何通过编程限制TextView的宽度,使其表现为字幕?
我正在从JSON文件创建一个项目列表,以便在我的应用程序上显示它们。但是我无法正确设置Android 如何通过编程限制TextView的宽度,使其表现为字幕?,android,kotlin,Android,Kotlin,我正在从JSON文件创建一个项目列表,以便在我的应用程序上显示它们。但是我无法正确设置文本视图,因此较长文本的部分会被隐藏,而不会溢出到多行,并表现为字幕 到目前为止,这是我的代码: val jsondata = Klaxon().parseArray<MTGSets>(jsonstr) for(mtgset: MTGSets in jsondata.orEmpty()) { val hlayout = LinearLayout(this)
文本视图
,因此较长文本的部分会被隐藏,而不会溢出到多行,并表现为字幕
到目前为止,这是我的代码:
val jsondata = Klaxon().parseArray<MTGSets>(jsonstr)
for(mtgset: MTGSets in jsondata.orEmpty()) {
val hlayout = LinearLayout(this)
val setTitle = TextView(this)
setTitle.text = mtgset.name
setTitle.ellipsize = MARQUEE
setTitle.maxWidth = 60.toPx()
setTitle.layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT)
hlayout.addView(setTitle)
cardsetlist.addView(hlayout)
}
val jsondata=Klaxon().parseArray(jsonstr)
for(mtgset:jsondata.orEmpty()中的mtgset){
val hlayout=线性布局(此)
val setTitle=TextView(此)
setTitle.text=mtgset.name
setTitle.ellipsize=字幕
setTitle.maxWidth=60.toPx()
setTitle.layoutParams=ViewGroup.layoutParams(ViewGroup.layoutParams.WRAP_内容,ViewGroup.layoutParams.MATCH_父项)
hlayout.addView(setTitle)
cardsetlist.addView(hlayout)
}
但文本会分成多行:
编辑
这是我正在寻找的结果,但从程序上看,正如您所看到的:
XML:
<TextView
android:id="@+id/textView4"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:ellipsize="marquee"
android:isScrollContainer="false"
android:marqueeRepeatLimit="marquee_forever"
android:singleLine="true"
android:text="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus gravida. " />
或
然后您可能需要增加文本视图的宽度-在您正在设置的文本视图上查找maxWidth参数setTitle.maxWidth=60.toPx()@dazza5000我认为这不是一个解决方案。我已经更新了我的问题,显示了我正在寻找的结果。
textView.maxLines = 1
textView.singleLine = true