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

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

在这个LinearLayout中,我有另一个LinearLayout,其方向为Horizo​​ntal,其中包含标题,....在这个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。

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

android文件添加一行代码怎么写,Android:以编程方式添加Textview,而不是将文本包装到下一行(示例代码)...相关推荐

  1. android 编译fastboot,Android应用开发之Fastboot烧写Android镜像文件到eMMC

    本文将带你了解Android应用开发之Fastboot烧写Android镜像文件到eMMC,希望本文对大家学Android有所帮助. < Fastboot烧写Android镜像文件到eMMC 以 ...

  2. adf435编程_动态ADF列车:以编程方式添加列车停靠点

    adf435编程 我将展示如何以编程方式"即时"将火车停靠站添加到ADF火车中. 在我的用例中,我有一些票务预订应用程序. 它具有训练模型的有限任务流. 在火车的第一站,用户输入乘 ...

  3. 动态ADF火车:以编程方式添加火车停靠站

    我将展示如何以编程方式"即时"将火车停靠站添加到ADF火车中. 在我的用例中,我有一些票务预订应用程序. 它具有训练模型的有限任务流. 在火车的第一站,用户输入乘客的数量,在随后的 ...

  4. android 搜索文件代码怎么写,android学习笔记(5)-一个搜索文件的APP(2)-搜索功能的实现...

    接上一篇,今天把搜索的代码放上去了.效果图如下. MainActivity.java package com.stk.afinder; import android.os.Bundle; import ...

  5. android中注册代码怎么写,Android P HIDL demo代码编写 (原创)

    之前的文章已经分析了hidl服务的注册和调用,这篇文章来总结下一个hidl的服务如何编写. 缩写hal文件 首先要确认放置文件夹和接口的包名,因为这跟后面使用脚本生成一部分代码有关,一般默认的放在ha ...

  6. android 文件加密解决方法,一种Android平台的文件快速加密以及解密方法与流程

    本发明属于数据安全领域,具体涉及一种Android平台的文件快速加密以及解密方法. 背景技术: 目前移动办公系统极大地提升了企.事业单位的工作效率,而带装有办公应用的智能设备中会存储一些较高机密性的文 ...

  7. android 文件的作用是什么意思,android手机刷机界面image 文件夹什么意思 里面的文件都是什么作用...

    image文件夹是用来存放android启动引导 boot.img . boot.img镜像不是普通意义上的文件系统,而是一种特殊的Android定制格式,由boot header,压缩的内核,ram ...

  8. android中文本框的居中,android – 以编程方式在TextView中居中文本

    我以编程方式创建了一个TextView,文本和背景可绘制.我想将文本居中在TextView中,但文本居于顶部. 这是我在TextView中居中文本的代码: protected class TileVi ...

  9. android的时间代码怎么写,Android 日期和时间的使用实例详解

    Android 日期和时间的使用 日期和时间的使用: 1:弹出框TimePickerDialog,DatePickerDialog 2:组件TimePicker,DatePicker TimePick ...

最新文章

  1. 更新pcb封装导入_PCB中3D应用相关功能详解
  2. python数据导出excel_python 数据生成excel导出(xlwt,wlsxwrite)代码实例
  3. Python之web开发(四):python使用django框架搭建网站之主页搭建
  4. OpenCV使用Facemark API
  5. QT的QBitArray 类的使用
  6. MYSQL 时间计算的 3 种函数
  7. 春运12306的bug
  8. Qt使用信号与槽时出现的错误“Incompatible sender/receiver arguments”
  9. LeetCode 537. 复数乘法
  10. Linux中7个判断文件系统类型的方法
  11. 一看数分就想写个程序玩玩。。。
  12. 随笔-Python批量转换图片格式
  13. 矩阵分析与应用-13-矩阵的迹
  14. 密西根大学张阳教授受聘中国上海交通大学客座教授(图)
  15. 性能问题从发现到优化一般思路
  16. arcgis pro 地图
  17. java 同步数据,同步数据到另一个库中。
  18. 手写的几个鼠标移上按钮的特效
  19. JDK11你必须了解的新特性
  20. sql脚本自动完成数据库创建、切换、建表、数据初始化

热门文章

  1. 云和恩墨大讲堂丨PostgreSQL逻辑复制案例分享
  2. 嘉年华回顾丨 尚博带你了解腾讯 TXSQL如何 赋能腾讯云分布式数据库 CynosDB
  3. 毫秒级从百亿大表任意维度筛选数据,是怎么做到的…
  4. 你真的懂Redis的5种基本数据结构吗?
  5. 释放千行百业数据价值,华为云DAYU有一套
  6. 稳坐开发领域霸主之位,揭秘C语言无可取代的几大原因!
  7. 技术实践丨基于MindSpore框架Yolov3-darknet模型的篮球动作检测体验
  8. 鲲鹏迁移第一批吃螃蟹的人,践行技术国际化
  9. 4j设置文件保存天数_文件备份很麻烦,各种工作不知道怎么选择,容器时代的备份方案!...
  10. 红橙Darren视频笔记 UML图简介