FrameLayout中的TextView被截断而不是包装

时间:2016-01-05 01:44:04

标签: android android-layout

我有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)之类的方法,但它们没有效果。

1 个答案:

答案 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可以解决问题为我。)