Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/191.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如果使用右页边距,行的最后一个字可能会导致不正确的textview维度_Android_Layout_Fonts_Textview_Bold - Fatal编程技术网

Android 如果使用右页边距,行的最后一个字可能会导致不正确的textview维度

Android 如果使用右页边距,行的最后一个字可能会导致不正确的textview维度,android,layout,fonts,textview,bold,Android,Layout,Fonts,Textview,Bold,在文本视图中使用粗体文本时,我有一种奇怪的行为 因此问题如下,我创建了一个自定义项,它保存文本,并在以后的列表中用作项,因此该项多次包含在scrollviews linearlayout中,它具有垂直方向 项目布局如下所示 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_he

在文本视图中使用粗体文本时,我有一种奇怪的行为

因此问题如下,我创建了一个自定义项,它保存文本,并在以后的列表中用作项,因此该项多次包含在scrollviews linearlayout中,它具有垂直方向

项目布局如下所示

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    style="@style/ButtonItem">
    <TextView
        android:id="@+id/m_oItem_TextView_TopLeft"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="20dp"
        android:layout_alignParentTop="true"
        android:layout_marginTop="10dp"
        android:textColor="@color/black"
        android:textSize="@dimen/font_size_12" />
    <TextView
        android:id="@+id/m_oItem_TextView_TopMiddle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/m_oItem_TextView_TopLeft"
        android:layout_toRightOf="@+id/m_oItem_TextView_TopLeft"
        android:layout_toLeftOf="@+id/m_oItem_TextView_TopRight"
        android:layout_marginRight="20dp"
        android:textColor="@color/black"
        android:textSize="@dimen/font_size_12"
        android:gravity="right"/>
    <TextView
        android:id="@+id/m_oItem_TextView_TopRight"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_marginRight="40dp"
        android:layout_alignTop="@+id/m_oItem_TextView_TopLeft"
        android:textColor="@color/black"
        android:textSize="@dimen/font_size_12"/>
    <TextView
        android:id="@+id/m_oItem_TextView_Bottom"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/m_oItem_TextView_TopLeft"
        android:layout_alignRight="@+id/m_oItem_TextView_TopRight"
        android:layout_below="@+id/m_oItem_TextView_TopLeft"
        android:layout_marginTop="5dp"
        android:textColor="@color/black"
        android:textSize="@dimen/font_size_14" />
    <ImageView 
        android:id="@+id/m_oItem_PlaceHolder"
        android:layout_width="wrap_content"
        android:layout_height="10dp"
        android:layout_alignLeft="@+id/m_oItem_TextView_TopLeft"
        android:layout_alignRight="@+id/m_oItem_TextView_TopRight"
        android:layout_below="@+id/m_oItem_TextView_Bottom"
        android:contentDescription="@string/image_description"/>
    <ImageView
        android:id="@+id/m_oPart_Arrow_Image"
        android:scaleType="fitCenter"
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:layout_centerVertical="true"
        android:layout_alignParentRight="true" 
        android:layout_marginRight="5dp"
        android:src="@drawable/arrow_right"
        android:contentDescription="@string/image_description"/>
</RelativeLayout>
文本当然会变得更宽,因此可能需要更多的行。现在的问题是,安卓不知何故不会考虑这一点

e、 g.如果非粗体行仅需要两行,如(下图中的最后一项)

粗体文本需要三行,但textview仍然只提供两行

有没有人遇到过同样的问题,或者有解决方案,或者至少有人建议我如何解决这个问题。无论如何,先谢谢你

编辑: 根据要求,ButtonItem样式如下

<style name="ButtonItem">
    <item name="android:background">@drawable/button_item</item>
</style>

@可拉拔/按钮项目
@可拉拔/按钮项目为:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true"  android:drawable="@drawable/button_item_down" />
    <item android:state_selected="true"  android:drawable="@drawable/button_item_selected" />
    <item android:drawable="@drawable/button_item_normal" />
</selector>

@可拉拔/按钮项目为:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:bottom="1dp"
        android:left="-2dp"
        android:right="-2dp"
        android:top="-2dp">
        <shape android:shape="rectangle">
            <solid android:color="@color/white" />
            <stroke
                android:width="@dimen/button_item_stroke_thickness"
                android:color="@color/black"/>
        </shape>
    </item>
</layer-list>

@可绘图/按钮项目所选为:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:bottom="1dp"
        android:left="-2dp"
        android:right="-2dp"
        android:top="-2dp">
        <shape android:shape="rectangle">
            <solid android:color="@color/white" />
            <stroke
                android:width="@dimen/button_item_stroke_thickness"
                android:color="@color/black"/>
        </shape>
    </item>
</layer-list>

@可拉拔/按钮项目正常为:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:bottom="1dp"
        android:left="-2dp"
        android:right="-2dp"
        android:top="-2dp">
        <shape android:shape="rectangle" >
            <solid android:color="@color/transparent" />
            <stroke
                android:width="@dimen/button_item_stroke_thickness"
                android:color="@color/black" />
        </shape>
    </item>
</layer-list>

基本上,它们只是在项目下方创建一条黑线,并在单击或选中时使背景变为白色。尽管“选定的打开”从未使用,因为“选定的状态”从未在代码中设置过

我也尝试过不以风格为背景,但正如预期的那样,问题仍然是一样的

再次编辑: 好的,所以我能够找到更多的问题,文本视图的大小调整实际上是正确的。具体的问题是以下几点。如果一行的最后一个字(例如,“这是一个测试”,而测试是最后一个字)导致换行,那么最后一个字需要足够长。我使用虚拟字符串测试这个问题,问题中使用了相同的项

这里有一些照片

正如您所看到的,导致问题的行还需要一个字符,那么单词就足够长了

上次编辑:
好的,我找到了答案(见下文)

文本视图正在重新调整大小,粗体文本正好合适,因此它是需要扩展以适应扩展文本视图的相对位置。使用LinearLayout(一个垂直,也许另一个水平,以匹配您使用RelativeLayout创建的设计)可能会更幸运,因为他们擅长在孩子发生变化时重新调整自己的大小


参考资料:

我终于找到了问题所在

这就是解决方案,问题是m_oItem_TextViewBottom的右边距,这是由以下原因引起的:

android:layout_alignRight="@+id/m_oItem_TextView_TopRight"
因此,它有效地创建了一个右边距,这是一个麻烦制造者,我用其他文本视图和右边距进行了测试,它们都有相同的问题

我用相同边距的填充物替换了它,并最终将其作为该项目的最终解决方案

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    style="@style/ButtonItem">
    <TextView
        android:id="@+id/m_oItem_TextView_TopLeft"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="20dp"
        android:layout_alignParentTop="true"
        android:layout_marginTop="10dp"
        android:textColor="@color/black"
        android:textSize="@dimen/font_size_12" />
    <TextView
        android:id="@+id/m_oItem_TextView_TopMiddle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/m_oItem_TextView_TopLeft"
        android:layout_toRightOf="@+id/m_oItem_TextView_TopLeft"
        android:layout_toLeftOf="@+id/m_oItem_TextView_TopRight"
        android:layout_marginRight="20dp"
        android:textColor="@color/black"
        android:textSize="@dimen/font_size_12"
        android:gravity="right"/>
    <TextView
        android:id="@+id/m_oItem_TextView_TopRight"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:paddingRight="40dp"
        android:layout_alignTop="@+id/m_oItem_TextView_TopLeft"
        android:textColor="@color/black"
        android:textSize="@dimen/font_size_12"/>
    <TextView
        android:id="@+id/m_oItem_TextView_Bottom"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/m_oItem_TextView_TopLeft"
        android:layout_below="@+id/m_oItem_TextView_TopLeft"
        android:layout_marginTop="5dp"
        android:paddingRight="40dp"
        android:textColor="@color/black"
        android:textStyle="bold" />
    <ImageView 
        android:id="@+id/m_oItem_PlaceHolder"
        android:layout_width="wrap_content"
        android:layout_height="10dp"
        android:layout_alignLeft="@+id/m_oItem_TextView_TopLeft"
        android:layout_alignRight="@+id/m_oItem_TextView_TopRight"
        android:layout_below="@+id/m_oItem_TextView_Bottom"
        android:contentDescription="@string/image_description"/>
    <ImageView
        android:id="@+id/m_oPart_Arrow_Image"
        android:scaleType="fitCenter"
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:layout_centerVertical="true"
        android:layout_alignParentRight="true" 
        android:layout_marginRight="5dp"
        android:src="@drawable/arrow_right"
        android:contentDescription="@string/image_description"/>
</RelativeLayout>


请提供style=“@style/ButtonItem”的详细信息。好吧,我会试一试,看看结果如何。好吧,我按照建议尝试了两个线性布局,但没有改变,仍然存在相同的问题。我还检查了textview的高度,它是29(一行)、53(两行)和77(三行),没有问题。如果线损坏,则高度不同,即44(两行)和68(三行)。因此,显然textview的大小调整不正确
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    style="@style/ButtonItem">
    <TextView
        android:id="@+id/m_oItem_TextView_TopLeft"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="20dp"
        android:layout_alignParentTop="true"
        android:layout_marginTop="10dp"
        android:textColor="@color/black"
        android:textSize="@dimen/font_size_12" />
    <TextView
        android:id="@+id/m_oItem_TextView_TopMiddle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/m_oItem_TextView_TopLeft"
        android:layout_toRightOf="@+id/m_oItem_TextView_TopLeft"
        android:layout_toLeftOf="@+id/m_oItem_TextView_TopRight"
        android:layout_marginRight="20dp"
        android:textColor="@color/black"
        android:textSize="@dimen/font_size_12"
        android:gravity="right"/>
    <TextView
        android:id="@+id/m_oItem_TextView_TopRight"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:paddingRight="40dp"
        android:layout_alignTop="@+id/m_oItem_TextView_TopLeft"
        android:textColor="@color/black"
        android:textSize="@dimen/font_size_12"/>
    <TextView
        android:id="@+id/m_oItem_TextView_Bottom"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/m_oItem_TextView_TopLeft"
        android:layout_below="@+id/m_oItem_TextView_TopLeft"
        android:layout_marginTop="5dp"
        android:paddingRight="40dp"
        android:textColor="@color/black"
        android:textStyle="bold" />
    <ImageView 
        android:id="@+id/m_oItem_PlaceHolder"
        android:layout_width="wrap_content"
        android:layout_height="10dp"
        android:layout_alignLeft="@+id/m_oItem_TextView_TopLeft"
        android:layout_alignRight="@+id/m_oItem_TextView_TopRight"
        android:layout_below="@+id/m_oItem_TextView_Bottom"
        android:contentDescription="@string/image_description"/>
    <ImageView
        android:id="@+id/m_oPart_Arrow_Image"
        android:scaleType="fitCenter"
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:layout_centerVertical="true"
        android:layout_alignParentRight="true" 
        android:layout_marginRight="5dp"
        android:src="@drawable/arrow_right"
        android:contentDescription="@string/image_description"/>
</RelativeLayout>