我有FrameLayout
即可动态添加TextViews
。目前,边缘附近的TextViews不包装在父FrameLayout的末尾,并且正在离开屏幕。 FrameLayout正在使用wrap_content
宽度和高度:
<FrameLayout
android:layout_margin="10dp"
android:id="@+id/wrappingFrameLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#00ff00"
>
正在创建TextViews
,然后使用.setTranslationX()
和.setTranslationY()
将其翻译为某个位置。
我已尝试使用SO上其他地方找到的答案进行换行,例如在TextView上将LayoutParams设置为WRAP_CONTENT:
newTextView.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT));
我也试过newTextView.setSingleLine(false);
,但那也没有用。
有什么想法吗?
编辑:所以我注意到动态添加的视图都没有响应它们上的任何方法,除了color / textsize之类的东西。我尝试调用setMinLines(3)
和.setWidth(100)
之类的方法,但它们没有效果。
答案 0 :(得分:0)
您可以尝试使用FrameLayout.MarginLayoutParams
代替.setTranslationX()
和.setTranslationY()
来放置TextViews
。
类似的东西:
FrameLayout.MarginLayoutParams layoutParams = (FrameLayout.MarginLayoutParams)textView.getLayoutParams();
layoutParams.leftMargin = xPosition;
layoutParams.topMargin = yPosition;
textView.setLayoutParams(layoutParams);
(我遇到了相反的问题。TextViews
正在包装,但我希望将它们切掉。我正在使用LayoutParams
和边距。更改为setTranslation
可以解决问题为我。)