我正在开发布局以显示信用卡的最后 4 位数字和卡类型图像。如果未选择任何卡,则会显示一些长度超过 4 位卡号的其他文本。选择卡片后,它会在 TextView
中正确显示,但是当设置较长的文本时,它不会正确显示。 TextView 不会为较长的文本扩展其宽度。
带有 4 位数卡号的图片
带有长文本的图像
这是完整的 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>
最佳答案
更新文本后,您是否尝试在 TextView 上调用这两个函数?
textView.invalidate();
textView.requestLayout();
说明:https://stackoverflow.com/a/42430695/1062074
已更新
以下内容将起作用,父布局上的 requestLayout 将起作用。
parentLayout.requestLayout();
关于android - 如果设置较长的文本, TextView 宽度不会改变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49029766/