1.如果TextView后面的控件是紧挨着TextView的,可以给TextView添加maxWidth限制其最大长度
上有问题的布局代码
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<import type="android.text.TextUtils" />
<variable
name="appCardEntity"
type="cn.com.westone.cxjr.cxsdk.model.AppCardEntity" />
<variable
name="adapter"
type="cn.com.westone.cx.platform.fragment.sub.card.AppCardKeyDataType.AppCardKeyDataAdapter" />
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/shape_search_bg"
android:orientation="vertical"
android:paddingHorizontal="16dp"
android:paddingVertical="8dp">
<LinearLayout
android:id="@+id/ll_title"
android:layout_width="match_parent"
android:layout_height="32dp"
android:gravity="center_vertical"
android:orientation="horizontal">
<ImageView
android:id="@+id/iv_icon"
android:layout_width="16dp"
android:layout_height="16dp"
android:padding="2dp"
android:src="@drawable/ic_placeholder" />
<com.westone.cx.commonsdk.uikit.scaleView.CXScaleTextView
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:gravity="center_horizontal"
android:maxLines="1"
android:text="@{TextUtils.isEmpty(appCardEntity.cardTitle)?appCardEntity.appName:appCardEntity.cardTitle}"
android:textColor="@color/cx_first_level_text_color"
android:textSize="@dimen/store_font_12" />
<ImageView
android:layout_width="16dp"
android:layout_height="16dp"
android:background="@drawable/arrow_right_icon"
android:padding="2dp" />
</LinearLayout>
<cn.com.westone.cx.platform.fragment.sub.component.NonScrollGridView
android:id="@+id/gv_data"
adapter="@{adapter}"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingVertical="16dp" />
</LinearLayout>
</layout>
有问题时展示效果图
解决方案布局代码
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<import type="android.text.TextUtils" />
<variable
name="appCardEntity"
type="cn.com.westone.cxjr.cxsdk.model.AppCardEntity" />
<variable
name="adapter"
type="cn.com.westone.cx.platform.fragment.sub.card.AppCardKeyDataType.AppCardKeyDataAdapter" />
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/shape_search_bg"
android:orientation="vertical"
android:paddingHorizontal="16dp"
android:paddingVertical="8dp">
<LinearLayout
android:id="@+id/ll_title"
android:layout_width="match_parent"
android:layout_height="32dp"
android:gravity="center_vertical"
android:orientation="horizontal">
<ImageView
android:id="@+id/iv_icon"
android:layout_width="16dp"
android:layout_height="16dp"
android:padding="2dp"
android:src="@drawable/ic_placeholder" />
<com.westone.cx.commonsdk.uikit.scaleView.CXScaleTextView
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:gravity="center_horizontal"
android:maxWidth="200dp" //关键代码
android:maxLines="1"
android:text="@{TextUtils.isEmpty(appCardEntity.cardTitle)?appCardEntity.appName:appCardEntity.cardTitle}"
android:textColor="@color/cx_first_level_text_color"
android:textSize="@dimen/store_font_12" />
<ImageView
android:layout_width="16dp"
android:layout_height="16dp"
android:background="@drawable/arrow_right_icon"
android:padding="2dp" />
</LinearLayout>
<cn.com.westone.cx.platform.fragment.sub.component.NonScrollGridView
android:id="@+id/gv_data"
adapter="@{adapter}"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingVertical="16dp" />
</LinearLayout>
</layout>
解决完成后效果图
2.TextView控件和其它控件分别在两端展示时,TextView把其它控件挤出屏幕
上有问题的布局代码
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<import type="android.view.View" />
<variable
name="appEntity"
type="cn.com.westone.cx.platform.fragment.sub.card.AppCardListDataType.AppCardListDataItemEntity" />
<variable
name="adapter"
type="cn.com.westone.cx.platform.fragment.sub.card.AppCardListDataType.AppCardListDataTypeAdapter" />
</data>
<RelativeLayout
android:id="@+id/ll_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:minHeight="32dp">
<TextView
android:id="@+id/tv_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:ellipsize="end"
android:gravity="left"
android:maxLines="1"
android:text="@{appEntity.text}"
android:textColor="@color/cx_first_level_text_color"
android:textSize="@dimen/store_font_12" />
<TextView
android:id="@+id/tv_time"
android:layout_width="65dp"
android:layout_height="wrap_content"
android:gravity="right"
android:layout_alignParentRight="true"
android:maxLines="1"
android:text="@{appEntity.formatTime()}"
android:textColor="@color/cx_second_level_text_color"
android:textSize="@dimen/store_font_12"
android:visibility="@{adapter.isHideTimeStamp?View.GONE:View.VISIBLE}" />
</RelativeLayout>
</layout>
有问题时展示效果图
使用LinearLayout与weight属性解决方案布局代码
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<import type="android.view.View" />
<variable
name="appEntity"
type="cn.com.westone.cx.platform.fragment.sub.card.AppCardListDataType.AppCardListDataItemEntity" />
<variable
name="adapter"
type="cn.com.westone.cx.platform.fragment.sub.card.AppCardListDataType.AppCardListDataTypeAdapter" />
</data>
<LinearLayout
android:id="@+id/ll_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:minHeight="32dp">
<TextView
android:id="@+id/tv_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ellipsize="end"
android:gravity="left"
android:maxLines="1"
android:text="@{appEntity.text}"
android:textColor="@color/cx_first_level_text_color"
android:textSize="@dimen/store_font_12" />
<TextView
android:id="@+id/tv_time"
android:layout_width="65dp"
android:layout_height="wrap_content"
android:gravity="right"
android:maxLines="1"
android:text="@{appEntity.formatTime()}"
android:textColor="@color/cx_second_level_text_color"
android:textSize="@dimen/store_font_12"
android:visibility="@{adapter.isHideTimeStamp?View.GONE:View.VISIBLE}" />
</LinearLayout>
</layout>
解决后效果图
原理是:LinearLayout里添加weight属性的控件会填满其它控件绘制完后剩余的全部空间。
仅以此记录一天的解决之路