简体   繁体   English

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

[英]TextView inside FrameLayout being cut-off and not wrapping

Ive got a FrameLayout which im dynamically adding TextViews to. 我有一个FrameLayout ,它动态添加TextViews Currently, the TextViews near the edge arent wrapping at the end of the parent FrameLayout and are going off screen. 目前,边缘附近的TextViews不包装在父FrameLayout的末尾,并且正在离开屏幕。 The FrameLayout is using wrap_content for both the width and height: 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"
        >

The TextViews are just being created, then translated to a certain spot using .setTranslationX() and .setTranslationY() . 正在创建TextViews ,然后使用.setTranslationX().setTranslationY()将其转换为某个位置。

I've tried getting it to wrap using some answers found elsewhere on SO, such as setting LayoutParams to WRAP_CONTENT on the TextView: 我尝试使用SO上其他地方找到的一些答案进行换行,例如在TextView上将LayoutParams设置为WRAP_CONTENT:

newTextView.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT));

I've also tried newTextView.setSingleLine(false); 我也试过newTextView.setSingleLine(false); but that didnt work either. 但那也没有用。

Any ideas? 有任何想法吗?

EDIT: So I noticed that none of the dynamically added views respond to any methods on them except things like color/textsize. 编辑:所以我注意到动态添加的视图都没有响应它们上的任何方法,除了color / textsize之类的东西。 I tried calling methods like setMinLines(3) and .setWidth(100) and they had no effect. 我尝试调用setMinLines(3).setWidth(100) ,但它们没有效果。

You can try using FrameLayout.MarginLayoutParams instead of .setTranslationX() and .setTranslationY() to position the TextViews . 您可以尝试使用FrameLayout.MarginLayoutParams而不是.setTranslationX().setTranslationY()来定位TextViews

Something like: 就像是:

FrameLayout.MarginLayoutParams layoutParams = (FrameLayout.MarginLayoutParams)textView.getLayoutParams();
layoutParams.leftMargin = xPosition;
layoutParams.topMargin = yPosition;
textView.setLayoutParams(layoutParams);

(I was having the opposite problem. The TextViews were wrapping but I wanted them to be cut-off. I was using the LayoutParams and margins. Changing to setTranslation fixed it for me.) (我遇到了相反的问题TextViews正在包装,但我希望它们被截断。我使用的是LayoutParams和边距。更改为setTranslation为我修复了它。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM