【问题标题】:TextView android:ellipsize="end" issueTextView android:ellipsize="end" 问题
【发布时间】:2012-03-26 11:04:27
【问题描述】:

在我的应用程序中,我使用了ellipsized 我的TextView,因此如果文本太大,它会在最后使用android:ellipsize="end" 显示...

例如,文本 ABCDEFGHIJKLMNOPQRSTUVWXYZ 显示为 ABCDEFGHIJ...。但是当文本很小时,例如ABCDEF,它仍然在末尾显示...,使文本看起来像AB...。我无法修复 textview 的宽度,因为在其他地方使用了相同的 textview,但有一些不同的宽度要求。我应该怎么做才能使其工作并仅在文本足够大时才显示...

【问题讨论】:

  • 向我们展示您用于 textview 的 XML。我感觉你在使用android:layout_width="wrap_content"
  • 你可以试试 android:ellipsize="marquee"。
  • android:layout_width="wrap_content" 它仍然不应该省略文本...也许任何父容器都有宽度限制..
  • @user1160207 没有称为多行的属性。

标签: android textview


【解决方案1】:

//在你的文本视图中

还添加以下属性

android:maxEms="8"
android:singleLine="true"

注意:您可以根据要显示的字符数调整 em 大小。

【讨论】:

  • 有用的答案。 EMS 不会直接转换为字符数。
【解决方案2】:
 txtvw.setText("The Indian economy is the world's eleventh-largest by nominal GDP and third-largest by purchasing power parity (PPP). " +
                "      Following market-based economic reforms in 1991, India became one of the fastest-growing major economies; " +
                "      it is considered a newly industrialised country. However, it continues to face the challenges of poverty, illiteracy, corruption, malnutrition, and inadequate public healthcare. " +
                "      A nuclear weapons state and a regional power, it has the third-largest standing army in the world and ranks ninth in military expenditure among nations." +
                "      India is a federal constitutional republic governed under a parliamentary system consisting of 28 states and 7 union territories. " +
                "      India is a pluralistic, multilingual, and multiethnic society. " +
                "      It is also home to a diversity of wildlife in a variety of protected habitats.");
        ViewTreeObserver vto = txtvw.getViewTreeObserver();
        vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

            @Override
            public void onGlobalLayout() {
                ViewTreeObserver obs = txtvw.getViewTreeObserver();
                obs.removeGlobalOnLayoutListener(this);
                if(txtvw.getLineCount() > 1){

                    int lineEndIndex = txtvw.getLayout().getLineEnd(1);
                    String text = txtvw.getText().subSequence(0, lineEndIndex-3)+"...";
                    txtvw.setText(text);

                }

            }
        });

【讨论】:

  • 这将我的行数减少到 2。它也可以设置为 3 行。并且 ellipsize 也可以正常工作。
猜你喜欢
  • 2023-03-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-20
  • 2016-03-09
  • 1970-01-01
  • 2013-12-06
相关资源
最近更新 更多