我正在尝试在android中扩展textview,我有一些带有一些批量内容的textview,我的要求是只显示批量内容中的两行和整理(...)。如果我点击它,我的全部内容将必须显示。如何 实现这一点。你能帮帮我吗?
public class MainActivity extends ActionBarActivity {
TextView t1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
t1 = (TextView) findViewById(R.id.text);
String str = "If your view subclass is displaying its own Drawable objects, it should override this function and return true for any Drawable it is displaying. This allows animations for those drawables to be scheduled." +
"I made the expandable list view acc to your tutorial but what i have to do if I want to populate Expandable List View with values from database?Means what changes I have to do in ";
t1.setText(str);
t1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
t1.setMaxLines(Integer.MAX_VALUE);
}
});
}
}
答案 0 :(得分:5)
默认情况下,您可以在布局xml ..
中将textview的android:maxLines
属性设置为2
将文本设置为textview(t1)后,在onCreate()函数中 添加以下
boolean isTextViewClicked = false;
t1.setOnClickListener(new OnClickListener() {
if(isTextViewClicked){
//This will shrink textview to 2 lines if it is expanded.
t1.setmaxLines(2);
isTextViewClicked = false;
} else {
//This will expand the textview if it is of 2 lines
t1.setMaxLines(Integer.MAX_VALUE);
isTextViewClicked = true;
}
});
这将根据内容
扩展textview答案 1 :(得分:0)
我在medium article中描述了最佳和优化方法。
您需要做的是在TextView.post {}函数中运行以下函数:
private fun TextView.setCaption(senderName: String, caption: String) {
text = getFullCaption(senderName, caption)
if (lineCount > DEFAULT_LINES) {
val lastCharShown = layout.getLineVisibleEnd(DEFAULT_LINES - 1)
maxLines = DEFAULT_LINES
val moreString = context.getString(R.string.read_more)
val suffix = " $moreString"
// 3 is a "magic number" but it's just basically the length of the ellipsis we're going to insert
val actionDisplayText = context.getString(R.string.more_dots) + suffix
text = SpannableStringBuilder()
.bold { append(senderName) }
.append(" ")
.append(
caption.substring(
0,
lastCharShown - suffix.length - 3 - moreString.length - senderName.length
)
)
.color(Color.GRAY) { append(actionDisplayText) }
}
}
在TextView的扩展功能中调用setCapton函数并设置单击侦听器以进行扩展,这是拥有可扩展TextView的最后一步:
private fun TextView.setExpandableText(senderName: String, caption: String) {
post {
setCaption(senderName, caption)
setOnClickListener {
let {
it.maxLines = MAX_LINES
it.text = getFullCaption(senderName, caption)
}
}
}
}