Android:文本视图动态显示多行中的文本取决于文本大小

时间:2016-05-06 19:29:19

标签: android textview

我的文字观看内容应该动态设置。取决于文本大小,它需要是多行的,并根据实际内容来调整大小。如果文本内容为null,则文本视图不应该是可见的

我试过了

           <TextView android:id="@+id/xyz"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:layout_marginTop="15dp"
                    android:textSize="21sp"
                    android:textColor="@color/white"                        
                    android:minLines="1"
                    android:maxLines="10"/>
            </LinearLayout>


 tvview = (TextView)findViewById(R.id.xyz);
 String tvtext = d.getText();//will get the content here
if (tvtext == null && tvtext .isEmpty()) {
    tvview.setVisibility(View.GONE);

} else {
   tvview.setVisibility(View.VISIBLE);

    tvview.setText(tvtext);
}

这是正确的做法吗?在这里,我如何决定文本视图应显示多少行取决于tvtext值。我只提到了xml文件中的min,max行。 android环境会照顾它吗?

请帮助如何实现它。

1 个答案:

答案 0 :(得分:0)

你这太复杂了。您无需指定行数。指定所需的宽度(通常为match_parent)并将高度设置为wrap_content。

<TextView android:id="@+id/xyz"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="15dp"
    android:textSize="21sp"
    android:textColor="@color/white"/>

tvview = (TextView)findViewById(R.id.xyz);
textView.setText("some text");

如果您的维度中至少有一个是warp_content,则会自动为您处理隐藏。