TextView 中心重力崩溃

Posted

技术标签:

【中文标题】TextView 中心重力崩溃【英文标题】:TextView Center Gravity Crash 【发布时间】:2014-04-22 12:57:49 【问题描述】:

嗨,我将背景设置为 textview

mImageAppInstallStatus.setBackgroundResource(R.drawable.downloaded);

然后我将textview的重力设置为中心如下

<TextView 
                    android:id="@+id/textview_price"
                    android:layout_
                    android:layout_
                    android:textSize="12dp"
                    android:textColor="#ffffff"
                    android:gravity="center"
                    android:layout_alignParentLeft="true"/>

但在设备

 java.lang.***Error
04-10 13:09:33.747: E/(6732):   at android.text.Layout.measureText(Layout.java:1655)
04-10 13:09:33.747: E/(6732):   at android.text.Layout.getLineMax(Layout.java:689)
04-10 13:09:33.747: E/(6732):   at android.text.Layout.draw(Layout.java:340)
04-10 13:09:33.747: E/(6732):   at android.text.BoringLayout.draw(BoringLayout.java:365)
04-10 13:09:33.747: E/(6732):   at android.widget.TextView.onDraw(TextView.java:4322)
04-10 13:09:33.747: E/(6732):   at android.view.View.draw(View.java:6920)
04-10 13:09:33.747: E/(6732):   at android.view.ViewGroup.drawChild(ViewGroup.java:1651)
04-10 13:09:33.747: E/(6732):   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
04-10 13:09:33.747: E/(6732):   at android.view.ViewGroup.drawChild(ViewGroup.java:1649)
04-10 13:09:33.747: E/(6732):   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
04-10 13:09:33.747: E/(6732):   at android.view.ViewGroup.drawChild(ViewGroup.java:1649)
04-10 13:09:33.747: E/(6732):   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
04-10 13:09:33.747: E/(6732):   at android.view.ViewGroup.drawChild(ViewGroup.java:1649)
04-10 13:09:33.747: E/(6732):   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
04-10 13:09:33.747: E/(6732):   at android.view.ViewGroup.drawChild(ViewGroup.java:1649)
04-10 13:09:33.747: E/(6732):   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
04-10 13:09:33.747: E/(6732):   at android.view.ViewGroup.drawChild(ViewGroup.java:1649)
04-10 13:09:33.747: E/(6732):   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
04-10 13:09:33.747: E/(6732):   at android.view.View.draw(View.java:6923)
04-10 13:09:33.747: E/(6732):   at android.widget.FrameLayout.draw(FrameLayout.java:357)
04-10 13:09:33.747: E/(6732):   at android.view.ViewGroup.drawChild(ViewGroup.java:1651)
04-10 13:09:33.747: E/(6732):   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
04-10 13:09:33.747: E/(6732):   at android.view.ViewGroup.drawChild(ViewGroup.java:1649)
04-10 13:09:33.747: E/(6732):   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
04-10 13:09:33.747: E/(6732):   at android.view.View.draw(View.java:6923)
04-10 13:09:33.747: E/(6732):   at android.view.ViewGroup.drawChild(ViewGroup.java:1651)
04-10 13:09:33.747: E/(6732):   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
04-10 13:09:33.747: E/(6732):   at android.widget.AbsListView.dispatchDraw(AbsListView.java:1487)
04-10 13:09:33.747: E/(6732):   at android.view.View.draw(View.java:7026)
04-10 13:09:33.747: E/(6732):   at android.widget.AbsListView.draw(AbsListView.java:2595)
04-10 13:09:33.747: E/(6732):   at android.view.ViewGroup.drawChild(ViewGroup.java:1651)
04-10 13:09:33.747: E/(6732):   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
04-10 13:09:33.747: E/(6732):   at android.view.View.draw(View.java:6923)
04-10 13:09:33.747: E/(6732):   at android.view.ViewGroup.drawChild(ViewGroup.java:1651)
04-10 13:09:33.747: E/(6732):   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
04-10 13:09:33.747: E/(6732):   at android.view.View.draw(View.java:6923)
04-10 13:09:33.747: E/(6732):   at android.view.ViewGroup.drawChild(ViewGroup.java:1651)
04-10 13:09:33.747: E/(6732):   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
04-10 13:09:33.747: E/(6732):   at android.view.ViewGroup.drawChild(ViewGroup.java:1649)
04-10 13:09:33.747: E/(6732):   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
04-10 13:09:33.747: E/(6732):   at android.view.ViewGroup.drawChild(ViewGroup.java:1649)
04-10 13:09:33.747: E/(6732):   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
04-10 13:09:33.747: E/(6732):   at android.view.View.draw(View.java:6923)
04-10 13:09:33.747: E/(6732):   at android.support.v4.view.ViewPager.draw(ViewPager.java:2153)
04-10 13:09:33.747: E/(6732):   at android.view.ViewGroup.drawChild(ViewGroup.java:1651)
04-10 13:09:33.747: E/(6732):   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
04-10 13:09:33.747: E/(6732):   at android.view.ViewGroup.drawChild(ViewGroup.java:1649)
04-10 13:09:33.747: E/(6732):   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
04-10 13:09:33.747: E/(6732):   at android.view.ViewGroup.drawChild(ViewGroup.java:1649)
04-10 13:09:33.747: E/(6732):   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
04-10 13:09:33.747: E/(6732):   at android.view.ViewGroup.drawChild(ViewGroup.java:1649)
04-10 13:09:33.747: E/(6732):   at android.support.v4.widget.DrawerLayout.drawChild(DrawerLayout.java:769)
04-10 13:09:33.747: E/(6732):   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
04-10 13:09:33.747: E/(6732):   at android.view.ViewGroup.drawChild(ViewGroup.java:1649)
04-10 13:09:33.747: E/(6732):   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
04-10 13:09:33.747: E/(6732):   at android.view.View.draw(View.java:6923)
04-10 13:09:33.747: E/(6732):   at android.widget.FrameLayout.draw(FrameLayout.java:357)
04-10 13:09:33.747: E/(6732):   at android.view.ViewGroup.drawChild(ViewGroup.java:1651)
04-10 13:09:33.747: E/(6732):   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
04-10 13:09:33.747: E/(6732):   at android.view.ViewGroup.drawChild(ViewGroup.java:1649)
04-10 13:09:33.747: E/(6732):   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)

【问题讨论】:

【参考方案1】:

您的 XML 可能太复杂了,提供额外的绘图命令可能会阻止您。

如果你有太多的嵌套布局视图,或者如果你有太多递归调用的方法,你会看到 java.lang.***Error 是很正常的。

查看Android Developers blog,了解如何理想地优化您的复杂布局。

另外不要忘记查看以下主题中关于嵌套布局数量的 CommonsWare 答案:

***: Caused by nested views?

【讨论】:

以上是关于TextView 中心重力崩溃的主要内容,如果未能解决你的问题,请参考以下文章

在textview中更改可绘制图像的重力

在代码中设置 textview 的边距和重力不起作用

TextView垂直重力在ScrollView中不起作用

TextView 提示弄乱了 RTL 重力

在 TextView 中使用区域设置 (ltr/rtl) 作为重力

单个textview中的两种不同风格,具有不同的重力和高度