简体   繁体   中英

TextView inside FrameLayout being cut-off and not wrapping

Ive got a FrameLayout which im dynamically adding TextViews to. Currently, the TextViews near the edge arent wrapping at the end of the parent FrameLayout and are going off screen. The FrameLayout is using wrap_content for both the width and height:

<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() .

I've tried getting it to wrap using some answers found elsewhere on SO, such as setting LayoutParams to WRAP_CONTENT on the TextView:

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

I've also tried 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. I tried calling methods like setMinLines(3) and .setWidth(100) and they had no effect.

You can try using FrameLayout.MarginLayoutParams instead of .setTranslationX() and .setTranslationY() to position the 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.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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