【问题标题】:Setting height of textview adds padding at top设置 textview 的高度在顶部添加填充
【发布时间】:2016-01-11 13:26:59
【问题描述】:

我在设置布局时遇到了一些问题,问题是用户简历的文本可能会有所不同,但使其成为 textview 高度,即下面代码中的 tvbio wrap_content 正在顶部添加填充,因此有效地干扰了所有布局,这是我的 xml

<LinearLayout
            android:id="@+id/userInfoHeadrLL"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:paddingLeft="5dip"
            android:paddingRight="10dip"
            android:paddingTop="0dip" >

            <RelativeLayout
                android:id="@+id/userInfoRL"
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:layout_marginTop="-50dip"
                android:orientation="vertical" >

                <ImageView
                    android:id="@+id/IVProfilePicture"
                    android:layout_width="120dp"
                    android:layout_height="120dip"
                    android:layout_alignParentStart="true"
                    android:layout_centerVertical="true"
                    android:background="@null"
                    android:paddingRight="6dip"
                    android:src="@drawable/anonymous_user" />

                <TextView
                    android:id="@+id/TVAmountEarned"
                    style="@style/small_body_text"
                    android:layout_width="wrap_content"
                    android:layout_height="40dip"
                    android:layout_below="@+id/IVProfilePicture"
                    android:layout_marginLeft="42dip"
                    android:gravity="center"
                    android:text="$0.00"
                    android:textColor="@color/grey" />

                <TextView
                    android:id="@+id/TVHolder"
                    style="@style/small_body_text"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_above="@+id/TVFullName"
                    android:layout_toRightOf="@+id/IVProfilePicture"
                    android:paddingBottom="-5dip"
                    android:text="samjaved"
                    android:textColor="@color/blue" />

                <TextView
                    android:id="@+id/TVFullName"
                    style="@style/small_body_text"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_above="@+id/simpleViewAboveTable"
                    android:layout_toRightOf="@+id/IVProfilePicture"
                    android:paddingBottom="0dip"
                    android:text="sami"
                    android:textColor="@color/grey" />

                <View
                    android:id="@+id/simpleViewAboveTable"
                    android:layout_width="fill_parent"
                    android:layout_height="1dp"
                    android:layout_above="@+id/tableUserInfoFollow"
                    android:layout_toRightOf="@+id/IVProfilePicture"
                    android:background="@color/llgrey" />

                <TableLayout
                    android:id="@+id/tableUserInfoFollow"
                    android:layout_width="fill_parent"
                    android:layout_height="45dip"
                    android:layout_centerVertical="true"
                    android:layout_marginBottom="2dip"
                    android:layout_toRightOf="@+id/IVProfilePicture"
                    android:stretchColumns="*" >

                    <TableRow
                        android:layout_width="wrap_content"
                        android:layout_height="45dip" >

                        <LinearLayout
                            android:layout_width="50dp"
                            android:layout_height="wrap_content"
                            android:layout_alignParentLeft="true"
                            android:layout_marginTop="8dip"
                            android:gravity="center"
                            android:orientation="vertical" >

                            <TextView
                                android:id="@+id/textPostedCount"
                                style="@style/small_body_text"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:text="0" />

                            <TextView
                                android:id="@+id/tvPosted"
                                style="@style/small_body_text"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_marginTop="-8dip"
                                android:text="Posted" />
                        </LinearLayout>

                        <LinearLayout
                            android:layout_width="50dp"
                            android:layout_height="wrap_content"
                            android:layout_alignParentLeft="true"
                            android:layout_marginTop="8dip"
                            android:gravity="center"
                            android:orientation="vertical" >

                            <TextView
                                android:id="@+id/textFollowersCount"
                                style="@style/small_body_text"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:text="0" />

                            <TextView
                                android:id="@+id/tvFollower"
                                style="@style/small_body_text"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_marginTop="-8dip"
                                android:text="Followers" />
                        </LinearLayout>

                        <LinearLayout
                            android:layout_width="50dp"
                            android:layout_height="wrap_content"
                            android:layout_alignParentLeft="true"
                            android:layout_marginTop="8dip"
                            android:gravity="center"
                            android:orientation="vertical" >

                            <TextView
                                android:id="@+id/textFollowedCount"
                                style="@style/small_body_text"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:gravity="center_vertical"
                                android:text="0" />

                            <TextView
                                android:id="@+id/tvFollowed"
                                style="@style/small_body_text"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_marginTop="-8dip"
                                android:gravity="center_vertical"
                                android:text="Followed" />
                        </LinearLayout>
                    </TableRow>
                </TableLayout>

                <View
                    android:id="@+id/simpleViewBelowLayout"
                    android:layout_width="fill_parent"
                    android:layout_height="1dp"
                    android:layout_below="@+id/tableUserInfoFollow"
                    android:layout_toRightOf="@+id/IVProfilePicture"
                    android:background="@color/llgrey" />

                <TextView
                    android:id="@+id/TVbio"
                    style="@style/small_body_text"
                    android:layout_width="fill_parent"
                    android:layout_height="200dip"
                    android:layout_below="@+id/simpleViewBelowLayout"
                    android:layout_marginTop="2dp"
                    android:layout_toRightOf="@+id/IVProfilePicture"
                    android:paddingBottom="0dip"
                    android:text="bio will go here sahsakjhdjahdjhdjhdjsahdjhahahdjhdsjhdajhdsjhdskjhakjhdskjashjkasjk" />
            </RelativeLayout>
        </LinearLayout>

为了解决这个问题,我目前正在检查代码中的生物文本并相应地设置高度和边距,但这似乎不是最好的方法,因为它在不同的屏幕上显示不同的结果,在这方面的任何帮助将不胜感激。

编辑

第一个是没有文本的期望行为

第二个是底部有文字时应该如何显示

第三个是当前没有文本时的显示方式,注意$0.00以下的额外空间

如何摆脱多余的空间?

如果要实现这一点,我会降低 textview 的高度,较大的文本会在它们开始被裁剪时出现问题。

【问题讨论】:

  • 你能用截图显示吗?
  • 你可以使用属性paddingTop来减少TextView的填充。
  • @drschultz 我实际上不知道我应该设置多少 paddingtop,因为 textview 的高度因文本而异
  • @chossen-addict - TextView 的高度无关紧要。它只是确定视图边缘和其中的文本之间有多少空间。如果这不是您的问题,那么您没有填充问题。您可以上传屏幕截图,以便我们实际查看问题所在。
  • @drschultz 截图已添加。

标签: android xml layout textview


【解决方案1】:

我认为设置android:layout_height="wrap_content" 绝对是 TVbio TextView 的最佳选择。您当然希望视图的大小根据您将收到的不同文本的数量来调整大小。所以我认为那部分是对的。

但是,如果您的文本在使用该属性时被截断,我会尝试设置android:layout_gravity="fill"。此属性告诉父级子视图应如何定位在容器内。我将从尝试fill 开始,如果这不起作用,您可以尝试其他一些。你可以找到all possible settings here的列表。

【讨论】:

  • 另外,您可以尝试在文本末尾添加几个 \n 字符,以尝试强制 View 的大小比您的实际文本内容大一点。
猜你喜欢
  • 2017-06-27
  • 2012-03-19
  • 2021-11-02
  • 1970-01-01
  • 2016-08-08
  • 2023-01-10
  • 1970-01-01
  • 2012-07-15
  • 1970-01-01
相关资源
最近更新 更多