【发布时间】:2015-08-23 09:49:26
【问题描述】:
我在 TextView 中有一个文本,我想在较大屏幕尺寸的设备上显示为单行,在较小屏幕尺寸的设备上显示为两行。我在它的右边也有一个图像。
现在的问题是 TextView 的大小比它里面的内容要大,即使它是一个包装内容,这会在 textview 和 image view 之间创建额外的不必要的空间。
我的代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="#FFFFFF"
android:gravity="center"
android:paddingLeft="40dp"
android:paddingRight="40dp"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Please Fit my width according tothecontentinsideme"
android:textColor="#000000"
android:background="#11DDFF"
android:textSize="22sp"
android:fontFamily="din-condensed"
android:layout_centerVertical="true"
android:layout_toLeftOf="@+id/rightArrowImage"
android:textStyle="bold"/>
<ImageView
android:id="@+id/rightArrowImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:src="@drawable/scoretracker_reedem_rewards_arrow"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
/>
</RelativeLayout>
</RelativeLayout>
更新
我尝试了dieter_h的建议
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="60dp"
android:background="#FFFFFF"
android:gravity="center"
android:paddingLeft="40dp"
android:paddingRight="40dp"
android:orientation="horizontal"
>
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Please Fit my width according tothecontentinsideme"
android:textColor="#000000"
android:background="#11DDFF"
android:textSize="22sp"
android:textStyle="bold"/>
<ImageView
android:id="@+id/rightArrowImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:src="@drawable/scoretracker_reedem_rewards_arrow"
/>
</LinearLayout>
</RelativeLayout>
立即输出:
【问题讨论】:
-
在 TextView 中而不是在 ImageView 中放置边距
-
在哪个布局中,线性布局还是相对布局?顺便说一句,我尝试了两种相同的结果,没有区别。
-
等一下,我会在几分钟内给出我的答案..
-
让我知道这就是你要的:)
-
好吧,如果我是对的,在不覆盖任何 View 方法的情况下,您可以减少边距和文本大小,或者为不同的屏幕尺寸创建不同的布局,让单行文本用于更大的尺寸和较小的 2 行。
标签: android android-layout textview