Android:以编程方式添加的 Textview 未将文本换行到下一行

Android: Programmatically added Textview not wrapping text to next line

我有点确定我做错了一些愚蠢的错误,但这是我的问题。

基本上我尝试使用卡片视图制作历史列表。在这些卡片视图中,我有一个垂直方向的 LinearLayout 来放置卡片的所有内容。

在这个 LinearLayout 中,我有另一个 LinearLayout,其方向为 Horizo​​ntal,其中包含标题,...。在这个 linearLayout 下方,我有一个包含实际字符串内容的文本视图。此内容非常大,由于某种原因,它没有换行到下一行。

也许这样的结构更容易理解:

卡片视图

_LinearLayout(垂直)

_ _ LinearLayout(水平)

_ _ _ 文本视图

_ _ TextView(这是有问题的那个)

我以编程方式设置这些组件,因为我不知道我必须事先打印多少张卡片。

代码如下:

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);

我的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;
    }

所以我尝试了很多在 Whosebug 和互联网上找到的东西。我发现很多但没有用的一些东西是:

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

我也一起尝试过其中一些。我真的希望有人能解决这个问题。哦,是的,它适用于 Android 5.1,但不适用于 Android 版本 7。

如果您仍在阅读这一行,我想明确感谢您提前投入时间和精力!

好的,对于遇到同样问题的任何人(我对此表示怀疑,但仍然)这就是为我解决的问题。我用一个函数(geefIntAsDp,我用另一个助手编写的)重新计算了视图的宽度和高度。这适用于普通整数,但不能与 wrap_content 或 match_parent 一起使用。所以我需要做的就是删除这个转换并且它起作用了。