android - 即使在包装内容后,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/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>

输出: Layout Image

更新

我尝试了 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>

现在输出:

enter image description here

最佳答案

试试这个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>

这是它的样子:

enter image description here

最终编辑

我做了一个新的 xml 试试这个

    <?xml version='1.0' encoding='utf-8'?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#fff"
    android:paddingLeft="20dp"
    android:paddingRight="20dp"
    android:layout_gravity="center_horizontal"
    android:orientation="horizontal" >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:background="#fff"
        android:gravity="center"
        android:orientation="horizontal"
        tools:ignore="DisableBaselineAlignment" >



        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"

            android:layout_weight="0.5"
            android:orientation="horizontal"
            >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="#000"
                android:textStyle="bold"
                android:textSize="15sp"
                android:background="#11DDFF"
                android:text="Please Fit my width according tothecontentinsideme"
                tools:ignore="HardcodedText"
                android:id="@+id/textViewTitleDialog"
                />

        </LinearLayout>
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"

            android:gravity="center"
            android:orientation="horizontal" >

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:contentDescription="@string/app_name"

                android:src="@mipmap/ic_launcher"
                android:id="@+id/asd" />
        </LinearLayout>
    </LinearLayout>

</LinearLayout>

这是输出

enter image description here

希望这是你想要的:)

关于android - 即使在包装内容后,TextView 宽度也不会调整为其中的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32165310/

相关文章:

java - 如何动态更改 Android 中 TextView 的行数?

android - 仅在文本本身上具有背景颜色的 TextView

android - 如何为 FirebaseMessagingService 实现 onDeletedMessages()?

android - 如何在android中的通知中打开应用程序?

android:如何在相对布局中将 block 居中?

android - 图像未完全显示在 Horizo​​ntalScrollView 中

android - 当字符串不是嵌入式资源时,如何设置 TextView 以显示 UTF-8?

android - CardView onTouch onClick

android - 如何获取不在Acitivty自身布局上的布局组件

android - 圆圈中的按钮