如果设置了较长的文本,则Textview宽度不会更改

问题描述 投票:1回答:2

我正在开发布局,以显示信用卡和卡片类型图像的最后4位数。如果没有选择任何卡,它将显示一些长于4位卡号的其他文本。选择卡后,它会在TextView中正确显示,但是当设置较长的文本时,它显示不正确。对于较长的文本,TextView不会扩展其宽度。

带4位数卡号的图像

enter image description here

图像与长文本

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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white">
<RelativeLayout
    android:id="@+id/headerLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingLeft="@dimen/margin_20"
    android:paddingRight="@dimen/margin_20"
    android:background="@color/ConfirmAndPayHeaderColor">
    <TextView
        android:id="@+id/CancelButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/CancelButton"
        android:background="@null"
        android:textColor="@drawable/TransparentButtonBgText"
        android:textAllCaps="false"
        android:textSize="@dimen/text_Size_17"
        android:paddingTop="@dimen/margin_15"
        android:paddingBottom="@dimen/margin_15"
        fontPath="@string/FontRobotoRegular"
        android:layout_centerVertical="true" />
    <TextView
        android:id="@+id/TitleText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/pay"
        android:layout_centerInParent="true"
        android:textColor="@color/black"
        android:textSize="@dimen/text_Size_17"
        android:paddingTop="@dimen/margin_15"
        android:paddingBottom="@dimen/margin_15"
        fontPath="@string/FontRobotoRegular" />
</RelativeLayout>
<View
    android:layout_width="match_parent"
    android:layout_height="@dimen/height_1"
    android:background="@color/DividerLightGrey" />
<RelativeLayout
    android:id="@+id/LocationLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingLeft="@dimen/margin_20"
    android:paddingRight="@dimen/margin_20">
    <TextView
        android:id="@+id/LocationLabel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/PickupLocation"
        android:textAllCaps="false"
        android:textSize="@dimen/text_Size_13"
        android:paddingTop="@dimen/margin_15"
        android:paddingBottom="@dimen/margin_15"
        android:layout_centerVertical="true"
        android:textColor="@color/lightGray"
        fontPath="@string/FontRobotoRegular" />
    <TextView
        android:id="@+id/ChangeButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/Change"
        android:textSize="@dimen/text_Size_13"
        android:background="@null"
        android:textColor="@drawable/TransparentButtonBgText"
        android:layout_alignParentRight="true"
        android:paddingTop="@dimen/margin_15"
        android:paddingBottom="@dimen/margin_15"
        android:layout_centerVertical="true"
        fontPath="@string/FontRobotoRegular" />
    <TextView
        android:id="@+id/LocationTitleText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Terrel Plaza"
        android:textSize="@dimen/text_Size_13"
        android:layout_centerHorizontal="true"
        android:textColor="@color/black"
        android:gravity="center"
        android:paddingTop="@dimen/margin_15"
        android:paddingLeft="@dimen/margin_3"
        android:paddingRight="@dimen/margin_3"
        android:layout_toRightOf="@id/pickupLocationLabel"
        fontPath="@string/FontRobotoBold"
        android:layout_toLeftOf="@id/ChangeButton" />
    <TextView
        android:id="@+id/LocationDetailText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="San Antonio, TX"
        android:textSize="@dimen/text_Size_13"
        android:gravity="center"
        android:layout_centerHorizontal="true"
        android:textColor="@color/lightGray"
        android:layout_below="@id/LocationTitleText"
        android:paddingBottom="@dimen/margin_15"
        android:paddingLeft="@dimen/margin_3"
        android:paddingRight="@dimen/margin_3"
        android:layout_toRightOf="@id/pickupLocationLabel"
        android:layout_toLeftOf="@id/ChangeButton"
        fontPath="@string/FontRobotoRegular" />
</RelativeLayout>
<View
    android:layout_width="match_parent"
    android:layout_height="@dimen/height_1"
    android:background="@color/DividerLightGrey"
    android:layout_marginLeft="@dimen/margin_20" />
<RelativeLayout
    android:id="@+id/paymentLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:paddingLeft="@dimen/margin_20"
    android:paddingRight="@dimen/margin_20"
    android:paddingBottom="@dimen/margin_15"
    android:paddingTop="@dimen/margin_15">
    <TextView
        android:id="@+id/paymentLabel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/Payment"
        android:textSize="@dimen/text_Size_13"
        android:textColor="@color/lightGray"
        fontPath="@string/FontRobotoRegular"
        android:paddingTop="@dimen/margin_3"
        android:layout_gravity="center_vertical"/>
    <LinearLayout
        android:id="@+id/paymentDetLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:orientation="horizontal"
        android:gravity="center_vertical">
        <ImageView
            android:id="@+id/cardImage"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:visibility="gone"
            android:src="@drawable/card_visa"
            android:layout_marginRight="@dimen/margin_8" />
        <TextView
            android:id="@+id/cardNoText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/AddCard"
            android:textColor="@color/black"
            android:textSize="@dimen/text_Size_13"
            android:layout_marginRight="@dimen/margin_10"
            fontPath="@string/FontRobotoRegular" />
        <ImageView
            android:id="@+id/paymentDropDown"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/down_arrow" />
    </LinearLayout>
</RelativeLayout>
<android.support.v7.widget.RecyclerView
    android:id="@+id/paymentListView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingLeft="@dimen/margin_10"
    android:paddingRight="@dimen/margin_10"
    android:visibility="gone"
    android:layout_below="@+id/paymentMethodLayout" />
<Button
    android:id="@+id/addPaymentTypeButton"
    android:layout_below="@id/paymentListView"
    android:layout_width="wrap_content"
    android:layout_marginBottom="@dimen/margin_10"
    android:layout_height="wrap_content"
    android:background="@null"
    android:textColor="@drawable/TransparentButtonBgText"
    android:textAllCaps="false"
    android:textSize="@dimen/text_Size_22"
    android:layout_centerHorizontal="true"
    android:layout_gravity="center"
    android:gravity="center"
    android:visibility="gone"
    fontPath="@string/FontUnited"
    android:text="@string/AddPaymentType" />
<View
    android:id="@+id/paymentDivider"
    android:layout_width="match_parent"
    android:layout_height="@dimen/height_1"
    android:background="@color/DividerLightGrey"
    android:layout_marginLeft="@dimen/margin_20" />
<RelativeLayout
    android:id="@+id/pickupTimeLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingLeft="@dimen/margin_20"
    android:paddingRight="@dimen/margin_20">
    <TextView
        android:id="@+id/pickUpTimeLabel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/PickupTime"
        android:textSize="@dimen/text_Size_13"
        android:paddingTop="@dimen/margin_15"
        android:paddingBottom="@dimen/margin_15"
        android:layout_centerVertical="true"
        android:textColor="@color/lightGray"
        fontPath="@string/FontRobotoRegular" />
    <TextView
        android:id="@+id/pickUpTimeText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="4:35 PM"
        android:textSize="@dimen/text_Size_12"
        android:textColor="@color/black"
        android:layout_alignParentRight="true"
        android:paddingTop="@dimen/margin_15"
        android:paddingBottom="@dimen/margin_15"
        android:layout_centerVertical="true"
        fontPath="@string/FontRobotoBold" />
</RelativeLayout>
<View
    android:layout_width="match_parent"
    android:layout_height="@dimen/height_1"
    android:background="@color/DividerLightGrey"
    android:layout_marginLeft="@dimen/margin_20" />
<RelativeLayout
    android:id="@+id/TotalLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingLeft="@dimen/margin_20"
    android:paddingRight="@dimen/margin_20">
    <TextView
        android:id="@+id/TotalLabel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/OrderTotal"
        android:textSize="@dimen/text_Size_13"
        android:paddingTop="@dimen/margin_15"
        android:paddingBottom="@dimen/margin_15"
        android:layout_centerVertical="true"
        android:textColor="@color/lightGray"
        fontPath="@string/FontRobotoRegular" />
    <TextView
        android:id="@+id/TotalText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="$5.77"
        android:textSize="@dimen/text_Size_13"
        android:textColor="@color/black"
        android:layout_alignParentRight="true"
        android:paddingTop="@dimen/margin_15"
        android:paddingBottom="@dimen/margin_15"
        android:layout_centerVertical="true"
        fontPath="@string/FontRobotoRegular" />
</RelativeLayout>
<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/PlaceOrder"
    android:id="@+id/placeOrderButton"
    android:textAllCaps="false"
    android:textSize="@dimen/text_Size_25"
    android:textStyle="bold"
    android:textColor="@color/white"
    android:background="@drawable/OrangeRectButton"
    android:layout_marginLeft="@dimen/margin_20"
    android:layout_marginRight="@dimen/margin_20"
    android:layout_marginTop="@dimen/margin_5"
    android:layout_marginBottom="@dimen/margin_15"
    fontPath="@string/FontUnited" />
</LinearLayout>

以下是仅针对我所面临的行的xml代码:

<RelativeLayout
    android:id="@+id/paymentLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:paddingLeft="@dimen/margin_20"
    android:paddingRight="@dimen/margin_20"
    android:paddingBottom="@dimen/margin_15"
    android:paddingTop="@dimen/margin_15">
    <TextView
        android:id="@+id/paymentLabel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/Payment"
        android:textSize="@dimen/text_Size_13"
        android:textColor="@color/lightGray"
        fontPath="@string/FontRobotoRegular"
        android:paddingTop="@dimen/margin_3"
        android:layout_gravity="center_vertical"/>
    <LinearLayout
        android:id="@+id/paymentDetLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:orientation="horizontal"
        android:gravity="center_vertical">
        <ImageView
            android:id="@+id/cardImage"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:visibility="gone"
            android:src="@drawable/card_visa"
            android:layout_marginRight="@dimen/margin_8" />
        <TextView
            android:id="@+id/cardNoText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/AddCard"
            android:textColor="@color/black"
            android:textSize="@dimen/text_Size_13"
            android:layout_marginRight="@dimen/margin_10"
            fontPath="@string/FontRobotoRegular" />
        <ImageView
            android:id="@+id/paymentDropDown"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/down_arrow" />
    </LinearLayout>
</RelativeLayout>
android layout textview
2个回答
2
投票

在更新文本后,您是否尝试在TextView上调用这两个函数?

textView.invalidate();
textView.requestLayout();

说明:https://stackoverflow.com/a/42430695/1062074

更新

以下工作,父布局上的requestLayout将起作用。

parentLayout.requestLayout(); 

0
投票
 please try this hope it will work.

   <RelativeLayout
   android:id="@+id/paymentLayout"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:gravity="center_vertical"
   android:paddingLeft="@dimen/margin_20"
   android:paddingRight="@dimen/margin_20"
   android:paddingBottom="@dimen/margin_15"
  android:paddingTop="@dimen/margin_15">
  <TextView
    android:id="@+id/paymentLabel"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/Payment"
    android:textSize="@dimen/text_Size_13"
    android:textColor="@color/lightGray"
    fontPath="@string/FontRobotoRegular"
    android:paddingTop="@dimen/margin_3"
    android:layout_gravity="center_vertical"/>
<LinearLayout
    android:id="@+id/paymentDetLayout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:orientation="horizontal"
    android:weightSum="3"
    android:gravity="center_vertical">
    <ImageView
        android:id="@+id/cardImage"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="gone"
        android:src="@drawable/card_visa"
        android:layout_marginRight="@dimen/margin_8" />
    <TextView
        android:id="@+id/cardNoText"
         android:layout_width="0dp"
        android:layout_weight="2"
        android:layout_height="wrap_content"
        android:text="@string/AddCard"
        android:textColor="@color/black"
        android:textSize="@dimen/text_Size_13"
        android:layout_marginRight="@dimen/margin_10"
        fontPath="@string/FontRobotoRegular" />
    <ImageView
        android:id="@+id/paymentDropDown"
        android:layout_width="0dp"
        android:layout_weight="2"
        android:layout_height="wrap_content"
        android:background="@drawable/down_arrow" />
</LinearLayout>

© www.soinside.com 2019 - 2024. All rights reserved.