android开发分享Android TextView文本没有被包装

任何人都可以告诉我什么是错的文字? 长于一行的文本不会换行到下一行,而是超出屏幕。

以下是代码:

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:padding="4dip"> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" android:padding="4dip"> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:padding="4dip"> <TextView android:id="@+id/reviewItemEntityName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="event/venue" android:textColor="@color/maroon" android:singleLine="true" android:ellipsize="end" android:textSize="14sp" android:textStyle="bold" android:layout_weight="1" /> <ImageView android:id="@+id/reviewItemStarRating" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_alignParentBottom="true" android:src="@drawable/title_1_star" /> </LinearLayout> <TextView android:id="@+id/reviewItemDescription" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Description comes here" android:textSize="12sp" android:layout_weight="1"/> </LinearLayout> </LinearLayout> 

    我自己修复了,关键是android:width="0dip"

     <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" android:padding="4dip" android:layout_weight="1"> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:padding="4dip"> <TextView android:id="@+id/reviewItemEntityName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@color/maroon" android:singleLine="true" android:ellipsize="end" android:textSize="14sp" android:textStyle="bold" android:layout_weight="1" /> <ImageView android:id="@+id/reviewItemStarRating" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_alignParentBottom="true" /> </LinearLayout> <TextView android:id="@+id/reviewItemDescription" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textSize="12sp" android:width="0dip" /> </LinearLayout> <ImageView android:id="@+id/widget01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/arrow_nxt" android:layout_gravity="center_vertical" android:paddingRight="5dip" /> </LinearLayout> 

    这个问题的唯一正确的答案是,你需要设置父母适当的宽度(在这种情况下FILL_PARENTWRAP_CONTENT )和使用android:layout_weight=1为需要被包装的WRAP_CONTENT

    SingleLine默认打开,所以不会做任何更改。

    width设置为0px将工作,但不是一个好的解决scheme。

    一些例子(这次在tableview中),使用xml:

     <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <TableLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:stretchColumns="*" android:id="@+id/tableLayout1"> <TableRow android:id="@+id/tableRow1" android:layout_width="fill_parent" android:layout_height="wrap_content"> <TextView android:text="test1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="0" /> <TextView android:layout_weight="1" android:text="test2 very long text that needs to be wrapped properly using layout_weight property and ignoring singleline since that is set by default..." android:layout_width="fill_parent" android:layout_height="wrap_content" /> </TableRow> </TableLayout> </LinearLayout> 

    如果你想在代码中设置它,你需要将layout_weight作为第三个参数,如下例所示:

     row.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); TextView label = new TextView(getApplicationContext()); label.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, 1f)); 

    您必须使用2个参数:

    在你的xml文件中使用就足够了。

     android:singleLine="false". 

    希望它能工作。

    祝一切顺利!

    使用TableLayout>TableRow>TextView时,我无法使用任何这些解决scheme。 然后我发现TableLayout.shrinkColumns="*" 。 唯一的解决scheme是强制TextView layout_width:250px等,但我不喜欢强制这样的宽度。

    如果使用表格尝试这样的事情。

      <TableLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:shrinkColumns="*"> 

    注意你需要shrinkColumns="*"

    这显然在一个<LinearLayout> 。 所以就像<LinearLayout> <TableLayout> <TableRow> <TextView>

    引用:

    TableLayout

    希望能帮助别人。

    您的代码中有一个布局参数是错误的。 在第一个TextView中

     android:layout_width="wrap_content" 

    改成

     android:layout_width="fill_parent" 

    超出屏幕宽度大小的文本将换行并设置android:singleline="false"

    对于我的情况删除inputtypes做了窍门,我使用android:inputType =“textPostalAddress”由于我的textview被粘贴到一行,并没有包装,解决这个问题。

    我是一个Android(和GUI)初学者,但有很多软件的经验。 我已经通过了一些教程,这是我的理解:

    layout_width和layout_height是TextView的属性。 它们是TextView如何形成自己的指令,并不是指如何处理TextView中的内容。

    如果使用“fill_parent”,则表示TextView应该相对于它的父视图进行自我形成,它应该填充它。

    如果使用“wrap_content”,则表示应该忽略父视图,并让TextView的内容定义它的形状。

    我认为这是令人困惑的地方。 “wrap_content”没有告诉TextView如何pipe理它的内容(包装行),它告诉它它应该相对于它的内容来塑造自己。 在这种情况下,没有新的行字符,它自己的形状,使所有的文字是在一个单一的行(不幸的是溢出父母)。

    我想你想要它水平填充父,并垂直包装它的内容。

    设置文本视图的高度android:minHeight="some pixes"android:width="some pixels" 。 这将解决问题。

    即使这是一个古老的线索,我想分享我的经验,因为它帮助了我。 我的应用程序工作正常的操作系统2.0和4.0 +,但对于运行OS 3.x的HTC手机文本不包装。 什么对我来说是包括这两个标签。

     android:maxLines="100" android:scrollHorizontally="false" 

    如果你消除了它只是没有工作的OS 3.0设备。 “ellipsize”参数有中性作用。 以下是完整的textview标签

     <TextView android:id="@+id/cell_description" android:layout_width="fill_parent" android:layout_height="wrap_content" android:paddingTop="5dp" android:maxLines="100" android:scrollHorizontally="false" android:textStyle="normal" android:textSize="11sp" android:textColor="@color/listcell_detail"/> 

    希望这将有助于某人。

    增加高度即。 android:height =“一些大小”,对我来说工作正常。

    我用android:ems="23"来解决我的问题。 在你的情况下,用最好的价值取代23。

     <TextView android:id="@+id/msg" android:ems="23" android:text="ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab " android:textColor="@color/white" android:textStyle="bold" android:layout_width="wrap_content" android:layout_height="wrap_content"/> 

    我有一个类似的问题,我的两个水平加权TextViews没有包装文本。 后来我发现这个问题是因为我的视图父母有wrap_content而不是match_parent。

    我认为这取决于显示器中布局的特定组合。 有些标志可能会被覆盖或忽略。 我有一个TabHost与选项卡,每个选项卡是一个表的列表。 所以它是ListView的一个选项卡,每一行都是TextView的TableLayout。 我尝试了上面列出的修复程序,没有一个工作。

    我把这个属性:

     android:inputType="textMultiLine" 

    到我的TextView,它有包装线和用户可以“input”一个新的一行。

    ================================================== ==========

    当你来到这篇文章时,你可能想要创build一个可以显示多行的Big TextView,所以这些属性也可能需要

     android:layout_height="Xdp" //where X is a number depends on how big Textview you want android:gravity="top" //in order to make your text start from the top of your TextView. 

    我知道,这是问题,这是正确的,但在我的情况下,问题是在'dp'中设置textSize属性 – 我已经将其更改为“sp”,它工作正常。

    在我的情况下,用TableRow > ScrollView > TextView嵌套,我通过在TableRow上设置android:layout_widthfill_parent ,在ScrollViewTextView上设置wrap_content来解决问题。

    你必须在你的TextView标签中使用android:singleLine="false"

    我终于设法添加一些像素到TextView的高度来解决这个问题。

    首先你需要得到TextView的高度。 这不是直接的,因为它已经被绘制之前是0。

    将此代码添加到onCreate:

     mReceiveInfoTextView = (TextView) findViewById(R.id.receive_info_txt); if (mReceiveInfoTextView != null) { final ViewTreeObserver observer = mReceiveInfoTextView.getViewTreeObserver(); observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { int height = mReceiveInfoTextView.getHeight(); int addHeight = getResources().getDimensionPixelSize(R.dimen.view_add_height); mReceiveInfoTextView.setHeight(height + addHeight); // Remove the listener if possible ViewTreeObserver viewTreeObserver = mReceiveInfoTextView.getViewTreeObserver(); if (viewTreeObserver.isAlive()) { viewTreeObserver.removeOnGlobalLayoutListener(this); } } }); } 

    您需要将此行添加到dimens.xml

     <dimen name="view_add_height">10dp</dimen> 

    希望它有帮助。

    我刚刚删除了android:lines="1"并添加了android:maxLines="2" ,这样文本就自动换行了。 问题是android:lines属性。 这导致文本包装不会发生。

    我没有必要使用maxEmssingleLine="false" (不build议使用的API)来解决这个问题。

    我在我的string中间添加了 n,看起来没问题。

    要包装文本,并把文本放在下一行,我们sholud使用“ n”即布局xml文件中的新行字符,并检查在模拟器上的更改不在布局屏幕上。

      以上就是android开发分享Android TextView文本没有被包装相关内容,想了解更多android开发(异常处理)及android游戏开发关注计算机技术网(www.ctvol.com)!)。

      本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

      ctvol管理联系方式QQ:251552304

      本文章地址:https://www.ctvol.com/addevelopment/518336.html

      (0)
      上一篇 2020年12月6日
      下一篇 2020年12月6日

      精彩推荐