我确定我做了一些愚蠢的错误,但这是我的问题。
基本上我尝试使用cardviews制作历史列表。在这些卡片视图中,我有一个垂直方向的LinearLayout,可以将我的所有内容都放入我的卡中。
在这个LinearLayout中,我有另一个LinearLayout,其方向为Horizontal,其中包含标题,....在这个linearLayout下面,我有一个textview,它包含实际的字符串内容。这个内容非常大,出于某种原因它不会包装到下一行。
也许这有点容易理解结构:
CardView
_ LinearLayout(垂直)
_ _ LinearLayout(水平)
_ _ _ Textview
_ _ TextView(这是有问题的那个)
我以编程方式设置这些组件,因为我不知道我需要预先打印多少张卡。
这是代码:
XML:
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">
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="start">
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">
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"/>
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;
}
所以我尝试了很多我在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。
如果你还在读这行,那么我想明确地感谢你提前花时间和精力!