简体   繁体   中英

Android: Programmatically added Textview not wrapping text to next line

I'm kinda sure I'm doing something stupid wrong but here's my problem.

Basically I try to make a history list using cardviews. In these cardviews I have a LinearLayout with a vertical orientation to put all my content for my card.

In this LinearLayout I have another LinearLayout with an orientation Horizontal which holds the title, ... . Below this linearLayout I have a textview which holds the actual string content. This content is quite large and for some kind of reason it doesn't wrap to the next line.

Maybe this is a bit easier to understand the structure:

CardView

_ LinearLayout (vertical)

_ _ LinearLayout (horizontal)

_ _ _ Textview

_ _ TextView (this is the one with the problems)

I set these components programmatically because I don't know how many cards I have to print beforehand.

Here is the code:

XML:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="url.Fragments.History_Fragment">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="start">

        <LinearLayout
            android:id="@+id/historiekLijst_Layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="start"
            android:layout_marginRight="5dp"
            android:orientation="vertical">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="5dp"
                android:gravity="start"
                android:text="Historiek"
                android:textStyle="bold"
                android:textSize="25dp"/>


        </LinearLayout>
    </ScrollView>
</LinearLayout>

History_Fragment class:

LinearLayout linearLayout = (LinearLayout) historiekView.findViewById(R.id.historiekLijst_Layout);
        CardView card = DynamicComponent_Helper.makeNewCardview(5, Gravity.CENTER_HORIZONTAL,Gravity.CENTER , 5, 15, getContext());

    TextView title = DynamicComponent_Helper.makeNewTextView(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, 5, Gravity.START, Gravity.START, 0, 20, "title",getContext());
                        title.setTypeface(null, Typeface.BOLD);
    TextView content= DynamicComponent_Helper.makeNewTextView(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, 5, Gravity.START, Gravity.START, 0, 15, "content which is too long and doesn't get wrapped",getContext());

LinearLayout titleLayout = DynamicComponent_Helper.makeNewLinearLayout(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.NO_GRAVITY, Gravity.NO_GRAVITY, LinearLayout.HORIZONTAL, getContext());
            LinearLayout cardLayout = DynamicComponent_Helper.makeNewLinearLayout(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.CENTER_HORIZONTAL, Gravity.START, LinearLayout.VERTICAL, getContext());

            titelLayout.addView(title);
            cardLayout.addView(titleLayout);
            cardLayout.addView(content);
            card.addView(cardLayout);
            linearLayout.addView(card);

My componentCreator_helper:

public static CardView makeNewCardview(int margin, int gravity,int layoutGravity, int radius, int padding, Context context){
// Initialize a new CardView
        CardView card = new CardView(context);

        // Set the CardView layoutParams
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT
        );
        params.setMargins(geefDPAlsInt(margin, context), geefDPAlsInt(margin, context), geefDPAlsInt(margin, context), geefDPAlsInt(margin, context));
        params.gravity = layoutGravity;
        card.setLayoutParams(params);
        // Set CardView corner radius
        card.setRadius(geefDPAlsInt(radius, context));

        // Set cardView content padding
        card.setContentPadding(geefDPAlsInt(padding, context), geefDPAlsInt(padding, context), geefDPAlsInt(padding, context), geefDPAlsInt(padding, context));

        // Set a background color for CardView
        card.setCardBackgroundColor(Color.parseColor("#FFFFFF"));

        // Set the CardView maximum elevation
        card.setMaxCardElevation(15);

        // Set CardView elevation
        card.setCardElevation(9);

        return card;

    }
public static LinearLayout makeNewLinearLayout(int width, int length, int gravity, int layoutGravity, int orientation, Context context){
        LinearLayout linearLayout = new LinearLayout(context);
        LinearLayout.LayoutParams layoutLinearParams =
                new LinearLayout.LayoutParams(geefDPAlsInt(width, context), geefDPAlsInt(length, context));
        layoutLinearParams.gravity = layoutGravity;
        linearLayout.setOrientation(orientation);
        linearLayout.setGravity(gravity);
        linearLayout.setLayoutParams(layoutLinearParams);

        return linearLayout;
    }
    public static ImageView makeNewImageView(String imageSource, int width, int length, int margin, int gravity, int layoutGravity, int tag, Context context){
        ImageView imageView = new ImageView(context);
        LinearLayout.LayoutParams imageLayout = new LinearLayout.LayoutParams(geefDPAlsInt(width, context), geefDPAlsInt(length, context));
        imageLayout.gravity = layoutGravity;
        imageLayout.setMargins(geefDPAlsInt(margin, context), geefDPAlsInt(margin, context), geefDPAlsInt(margin, context), geefDPAlsInt(margin, context));
        imageLayout.gravity = gravity;
        imageView.setLayoutParams(imageLayout);
        imageView.setImageResource(context.getResources().getIdentifier(imageSource, "drawable", context.getPackageName()));
        imageView.setTag(tag);

        return imageView;
    }
    public static TextView makeNewTextView(int width, int length, int margin, int gravity, int layoutGravity, int tag, int textsize, String tekst, Context context){
        TextView tekstView = new TextView(context);
        LinearLayout.LayoutParams layoutTextParams = new LinearLayout.LayoutParams(geefDPAlsInt(width, context), geefDPAlsInt(length, context));
        layoutTextParams.setMargins(geefDPAlsInt(margin, context), geefDPAlsInt(margin, context), geefDPAlsInt(margin, context), geefDPAlsInt(margin, context));
        layoutTextParams.gravity = layoutGravity;
        tekstView.setLayoutParams(layoutTextParams);
        tekstView.setGravity(gravity);
        tekstView.setText(tekst);
        tekstView.setTextSize(TypedValue.COMPLEX_UNIT_SP, textsize);
        tekstView.setTag(tag);
        return tekstView;
    }

So I have tried a lot of stuff that I found on stackoverflow and else on the internet. A few things that I found a lot but didn't work are:

content.setMaxWidth(0) => my cards have no more content and the height becomes WAY to big

content.setWidth(0) => same result
content.setInputType(InputType.TYPE_TEXT_FLAG_IME_MULTI_LINE); => no change
content.setSingleLine(false); => no change
content.setMaxLines(10) => no change
content.setHorizontallyScrolling(false); => no change

I have tried some of these together as well. I really hope someone could fix this. Oh yea it works on Android 5.1 but not on Android version 7.

If you're still reading at this line than I would like to explicitly thank you for you're time and effort in advance!

Alright for anyone who has the same problem (I doubt it but still) Here is what fiex it for me. I recalculated the Width and Height of my views to dip's with a function (geefIntAsDp which I have written in a different helper). This works fine with normal Integers but you can't use it with wrap_content or match_parent. So all I needed to do was delete this conversion and it worked.

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