Android:以编程方式添加Textview,而不是将文本包装到下一行

时间:2017-12-16 23:15:09

标签: android textview textwrapping programmatically-created programmatically

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

基本上我尝试使用cardviews创建历史列表。在这些卡片视图中,我有一个垂直方向的LinearLayout,可以将我的所有内容都放入我的卡片中。

在这个LinearLayout中,我有另一个LinearLayout,其方向为Horizo​​ntal,其中包含标题,....在这个linearLayout下面,我有一个textview,它包含实际的字符串内容。这个内容非常大,出于某种原因,它不会包装到下一行。

也许这对结构更容易理解:

CardView

_ LinearLayout(垂直)

_ _ LinearLayout(水平)

_ _ _ Textview

_ _ 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;
    }

所以我尝试了很多我在stackoverflow上找到的东西,还有互联网上的东西。我发现很多但没有成功的一些事情是:

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。

如果你仍然在阅读此行,那么我想明确地感谢你提前花时间和精力!

1 个答案:

答案 0 :(得分:1)

对于那些有同样问题的人(我怀疑但是仍然存在),这是正确的。这对我来说是个好消息。我重新计算了我的观点的宽度和高度,用一个函数(我用不同的帮助器编写的geefIntAsDp)。这适用于普通的整数,但你不能将它与wrap_content或match_parent一起使用。所以我需要做的就是删除这个转换并且它有效。