Android:以编程方式添加 Textview 不将文本换行到下一行
Posted
技术标签:
【中文标题】Android:以编程方式添加 Textview 不将文本换行到下一行【英文标题】:Android: Programmatically added Textview not wrapping text to next line 【发布时间】:2017-12-16 23:15:08 【问题描述】:我有点确定我做错了什么愚蠢的事情,但这是我的问题。
基本上,我尝试使用卡片视图制作历史列表。在这些卡片视图中,我有一个垂直方向的 LinearLayout,可以将我的所有内容放入卡片中。
在此 LinearLayout 中,我有另一个 LinearLayout,其方向为 Horizontal ,其标题为 ... 。在这个 linearLayout 下面我有一个 textview 保存实际的字符串内容。此内容非常大,由于某种原因它没有换行到下一行。
也许这样结构更容易理解:
卡片视图
_线性布局(垂直)
_ _ 线性布局(水平)
_ _ _ 文本视图
_ _ TextView(这是有问题的)
我以编程方式设置这些组件,因为我不知道我必须事先打印多少张卡片。
代码如下:
XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_
android:layout_
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_
android:layout_
android:layout_gravity="start">
<LinearLayout
android:id="@+id/historiekLijst_Layout"
android:layout_
android:layout_
android:layout_gravity="start"
android:layout_marginRight="5dp"
android:orientation="vertical">
<TextView
android:layout_
android:layout_
android:layout_margin="5dp"
android:gravity="start"
android:text="Historiek"
android:textStyle="bold"
android:textSize="25dp"/>
</LinearLayout>
</ScrollView>
</LinearLayout>
History_Fragment 类:
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;
所以我尝试了很多我在 *** 和互联网上找到的东西。我发现了很多但没有用的一些东西是:
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。
如果您仍在阅读此行,我想明确感谢您提前付出的时间和精力!
【问题讨论】:
您是否尝试过如here 所述以编程方式将布局权重设置为 1? 我现在试过了,但它并没有改变任何东西 【参考方案1】:对于遇到相同问题的任何人都可以(我对此表示怀疑,但仍然如此)这是为我解决的问题。我用一个函数(geefIntAsDp,我用不同的助手编写)重新计算了我的视图的宽度和高度。这适用于普通整数,但不能与 wrap_content 或 match_parent 一起使用。所以我需要做的就是删除这个转换并且它起作用了。
【讨论】:
以上是关于Android:以编程方式添加 Textview 不将文本换行到下一行的主要内容,如果未能解决你的问题,请参考以下文章
以编程方式在android中将下划线文本设置为TextView
Android - 以编程方式设置 TextView TextStyle?