我有一个LinearLayout
,其中包含一个TextView
:
<LinearLayout
android:id="@+id/addTaskDescriptionLLId"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp">
<TextView
android:id="@+id/descriptionTextViewId"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxHeight="150dp"
android:hint="@string/abcd"
android:textColor="#A2A2A2"
android:ellipsize="end"
android:layout_margin="10dp"/>
</LinearLayout>
我还在Java文件中添加了一个条件,条件是如果TextView
在打开活动期间有一些文本,那么它将可见,否则我将使用descriptionTextView.setVisibility(View.GONE);
然后我通过打开一个对话框在textView
中放入一些文本,然后单击onConfirm
(单击对话框的肯定按钮),我做了以下操作:
@Override
public void onConfirm(String description) {
this.description = description; //the string that I write in dialog
descriptionLL.removeAllViews(); //parent LineanLayout of TextView
descriptionLL.addView(descriptionTextView);
descriptionTextView.setText(description);
descriptionTextView.setVisibility(View.VISIBLE);
}
但是,正如我在xml文件中提到的,TextView的高度并没有保持wrap_content。我也尝试过:
descriptionTextView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
那也行不通。任何解决方案?
我认为您在xml
中指定的提示导致了问题。删除它,可能会解决。