android - 如何在android中只展开textview

标签 android textview expandable

我正在尝试在 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);
    }
 });
}
}

最佳答案

您可以在布局 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

关于android - 如何在android中只展开textview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28147720/

相关文章:

Android: java.lang.ClassCastException: android.widget.imageView 不能转换为 android.widget.textView

android - 多扩展回收 View

swift - 可扩展部分 UITableView IndexPath SWIFT

android - 如何检测ScrollView是否正在滚动

android - 两个图像拖动

Android:如何将变量值存储到注册表?

android - CodenameOne:VKB换屏

android - 如何使用支持库字体功能作为 TextView 内容的一部分(使用 spannable)?

android - 在 Android DialogFragments 中高效显示大型 TextView

带有扩展面板行的 ASP.net 2.0 Gridview -- 如何构建面板 "on the fly"