Android TextView宽度不会随着其内部内容而调整,即使在包装内容之后也是如此
我在TextView中有一个文本,我想在大屏幕设备上显示单行,在小屏幕设备上显示两行。我在它的右边也有一个图像 现在的问题是TextView的大小越来越大,甚至它是一个包装内容,这会在TextView和image view之间产生额外的不必要的空间 我的代码:Android TextView宽度不会随着其内部内容而调整,即使在包装内容之后也是如此,android,android-layout,textview,Android,Android Layout,Textview,我在TextView中有一个文本,我想在大屏幕设备上显示单行,在小屏幕设备上显示两行。我在它的右边也有一个图像 现在的问题是TextView的大小越来越大,甚至它是一个包装内容,这会在TextView和image view之间产生额外的不必要的空间 我的代码: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/andro
<?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>
输出:
更新
我尝试了迪特尔的建议
<?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>
立即输出:
<?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>
试试这个
xml
<?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="20dp"
android:paddingRight="20dp"
android:orientation="horizontal"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="301dp"
android:layout_height="wrap_content"
android:text="Please Fit my width according tothecontentinsideme"
android:textColor="#000000"
android:id="@+id/TextView2"
android:background="#11DDFF"
android:textSize="22sp"
android:textStyle="bold"/></LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/rightArrowImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"
/>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
这是输出
希望它是您想要的:)在文本视图而不是图像视图中放置边距在哪个布局中,线性布局还是相对布局?顺便说一句,我尝试了两种相同的结果,没有差别。请稍候,我会在几分钟内给出我的答案。谢谢,我正在等待。如果我是对的,在不覆盖任何查看方法的情况下,您可以减少页边距和文本大小,或者为不同的屏幕大小创建不同的布局,允许一行文本用于较大的大小,两行文本用于较小的大小。谢谢,我已经复制粘贴了您的代码,但这里的结果与我之前的结果相同。我想知道为什么?把你的整个xml放一次。嘿,你已经固定了宽度,我知道这个窍门,但问题是对于更大的屏幕尺寸,它不会出现在单行中,这是我所需要的。试试我的旧的,如果你放10sp,它将适合不,我不想这样。左图中多余的空间我不想要。宽度应为,直到文本结束。我认为使用xml无法解决这个问题。需要重写TextView onMeasure(),但我不知道如何才能实现所需的结果。。我尽了最大的努力我没有主意了。。。如果要覆盖onMeasure(),只需查看以下答案,