【问题标题】:Text View surrounded by Rectangular Shape has extra space in layout被矩形包围的文本视图在布局中有额外的空间
【发布时间】:2021-10-03 06:20:16
【问题描述】:

这是我今天早些时候提出的问题: Recreate shape as in xml file by using code and set width programmatically

使用提供的上述解决方案,我能够获得所需的结果,但有一个小问题我无法弄清楚。请帮帮我。

无论文本长度如何,矩形框都应该包裹文本视图。但是当内容比同一个视图中的其他文本长时,就会显示间隙。

这是加载到回收器视图中的布局代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:id="@+id/lin1"
    android:weightSum="2">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/rel1"
        android:layout_marginLeft="5dp"
        android:layout_weight="1.50">
        <TextView
            android:id="@+id/list"
            android:textSize="@dimen/grid_row_text_size"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:minWidth="100dp"
            android:textColor="@color/orange"
            android:textAlignment="center"
            android:singleLine="true"
            android:layout_marginRight="3dp"
            android:gravity="center">

        </TextView>

    </RelativeLayout>

    <ImageView
        android:id="@+id/info_icn"
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:layout_alignParentBottom="true"
        android:src="@drawable/eye"
        android:visibility="gone"
        android:layout_weight="0.50"
        android:layout_gravity="bottom"

        />

</LinearLayout>

This is the result which I am getting now

For more clearance on the issue, another pic of the above

This is the result I get after changing the top Linear Layout (android:layout_width="match_parent")

如果需要更多代码,我可以在这里发布。请在这个问题上帮助我。非常感谢!

【问题讨论】:

    标签: android android-recyclerview textview android-shapedrawable


    【解决方案1】:

    无论文本长度如何,矩形框都应该包裹文本视图 也许。但是当内容比同一个视图中的其他文本长时, 显示间隙。

    要使 TextView 包装其内容,您必须对 RecyclerView 项目布局进行一些更改:

    1. RelativeLayout (@+id/rel1) 具有 android:layout_width="match_parent" 并且必须是 wrap_content。
    2. TextView (@+id/list) 具有 android:layout_width="match_parent" 并且必须是 wrap_content。
    3. TextView (@+id/list) 的 android:minWidth="100dp" 会在内容太小时影响 TextView 包裹到其内容。

    我已将您的 xml 布局修改为 Wrap 到其内容,如下例所示:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/lin1"
        android:layout_width="wrap_content"
        android:layout_height="100dp"
        android:background="@android:color/transparent"
        android:orientation="horizontal">
    
        <RelativeLayout
            android:id="@+id/rel1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@android:color/transparent"
            android:layout_gravity="center"
            android:layout_marginStart="5dp"
            android:layout_marginTop="5dp"
            android:layout_marginBottom="5dp">
    
            <TextView
                android:id="@+id/list"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="@dimen/grid_row_text_size"
                android:text="Test text"
                android:gravity="center"
                android:layout_centerInParent="true"
                android:textColor="@android:color/holo_orange_dark"
                android:background="@android:color/transparent"
                android:textAlignment="center"
                android:singleLine="true"
                android:paddingTop="10dp"
                android:paddingBottom="10dp"
                android:paddingStart="5dp"
                android:paddingEnd="5dp"/>
    
        </RelativeLayout>
    
        <ImageView
            android:id="@+id/info_icn"
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:src="@drawable/eye"
            android:visibility="gone"
            android:layout_gravity="bottom" />
    
    </LinearLayout>
    

    根据您的问题,TextView 具有矩形形状作为背景,带有 Cut TopRightCorner。使用 ShapeAppearanceModel 以编程方式添加 TextView 背景后,您应该得到您想要的结果,如下所示:

    【讨论】:

    • 非常感谢! MariosP ..我发现问题出在代码上。在代码中,使用了GridLayoutManager,这导致了文本之间的间隙(即选择了具有最大长度的文本,并且将其父级的宽度设置为所有其他文本的父级宽度)。现在我用 LinearLayoutManager 替换了 GridLayoutManager,我能够获得正确的结果。非常感谢您的帮助!
    猜你喜欢
    • 2016-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-03
    • 2021-12-26
    • 1970-01-01
    • 2021-09-16
    • 2014-11-18
    相关资源
    最近更新 更多