问题描述
这是我今天早些时候问的问题: 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")
如果需要更多代码,我可以在这里发布。请帮助我解决这个问题。非常感谢!
解决方法
无论文本长度如何,矩形框都应该包裹文本视图 也许。但是当内容比同一视图中的其他文本长时, 显示间隙。
要使 TextView 环绕其内容,您必须对 RecyclerView Item 布局进行一些更改:
-
RelativeLayout (@+id/rel1)
具有 android:layout_width="match_parent",并且必须是 wrap_content。 -
TextView (@+id/list)
具有 android:layout_width="match_parent",并且必须是 wrap_content。 -
TextView (@+id/list)
具有 android:minWidth="100dp",当内容太小时会影响 TextView 换行到其内容。
我已将您的 xml 布局修改为包装到其内容,如下例所示:
<?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 背景后,您应该得到如下所示的结果: