ConstraintLayout两个Textview,第二个Textview被挤出屏幕外的解决方案

遇到的一个问题:

同一行的两个Textview,要实现两个View连着,前一个view的内容长度不确定,过长的时候会导致第二个view被挤出屏幕外,使用LinearLayout也会出现这种情况。

这里提供一个解决方案:

借助于辅助线,和

app:layout_constrainedHeight表示是否约束height,相应的也有app:layout_constrainedWidth

app:layout_constraintVertical_bias表示作用于链头第一个子控件,通过设置值0~1控制头尾间距比例(偏移比例)

这里贴上一个拖拽UI的界面图。


垂直方向,借助于3条垂直辅助线。从左至右,后续依次称为,guideline1,guideline2, guideline3。两个textview依次称为t1,t2。

要实现t2紧跟t1,则t2左侧的依赖关系就要依赖于t1的右侧,当文字内容较短的时候,t2可以实现紧跟t1。

但当t1文字内容过长,就会把t2顶出屏幕外。

但是这个时候t2是紧贴屏幕右侧的

往往我们都需要t2右侧有一定的间距


如上图,t2与第二条辅助线右侧建立关系。此时不存在guideline2,这事就会出现t1过长直接把t2挤出去的情况。

因为t2的左侧已经和t1的右侧建立了关系,这个时候t1的右侧已经不能再与t2的左侧建立关系。这时,我们添加一条辅助线guideline2。

让t1的右侧与guideline2建立关系。但是这个时候,当t1文字过长的时候,不会再将t2挤出屏幕外了,但是会覆盖住t2。

这个时候给t1 加入app:layout_constrainedWidth=true,这个属性,可以防止t2被覆盖。

但是还有一个问题,t1上下左右全部加入了依赖关系,当文字内容过短的时候,文字出现了居中的现象,这显然不是我们想要的效果。

这时候,就需要引入app:layout_constraintHorizontal_bias="0.0",让t1居左显示(1.0是居右显示)

最后附上代码(虽说是界面化操作,但是有的时候界面和代码同时使用能更好的解决问题):

<?xml version="1.0" encoding="utf-8"?>

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"

   xmlns:app="http://schemas.android.com/apk/res-auto"

   xmlns:tools="http://schemas.android.com/tools"

   android:id="@+id/nim_pop_notice_layout"

   android:layout_width="match_parent"

   android:layout_height="wrap_content">

       android:id="@+id/guideline21"

       android:layout_width="wrap_content"

       android:layout_height="wrap_content"

       android:orientation="vertical"

       app:layout_constraintGuide_end="88dp" />

       android:id="@+id/guideline3"

       android:layout_width="wrap_content"

       android:layout_height="wrap_content"

       android:orientation="vertical"

       app:layout_constraintGuide_end="37dp" />

       android:id="@+id/guideline2"

       android:layout_width="wrap_content"

       android:layout_height="wrap_content"

       android:orientation="vertical"

       app:layout_constraintGuide_begin="37dp" />

       android:id="@+id/guideline4"

       android:layout_width="wrap_content"

       android:layout_height="wrap_content"

       android:orientation="horizontal"

       app:layout_constraintGuide_begin="52dp" />

       android:id="@+id/vx_content"

       android:layout_width="wrap_content"

       android:layout_height="wrap_content"

       android:ellipsize="end"

       android:gravity="center_vertical"

       android:includeFontPadding="false"

       android:maxLines="1"

       android:text="@string/vx_content"

       android:textColor="@color/black_333333"

       android:textSize="@dimen/common_title_size16"

       app:layout_constrainedWidth="true"

       app:layout_constraintBottom_toTopOf="@+id/guideline5"

       app:layout_constraintEnd_toStartOf="@+id/guideline21"

       app:layout_constraintHorizontal_bias="0.0"

       app:layout_constraintStart_toStartOf="@+id/guideline2"

       app:layout_constraintTop_toTopOf="@+id/guideline4" />

       android:id="@+id/vx_change"

       android:layout_width="wrap_content"

       android:layout_height="wrap_content"

       android:layout_gravity="center_vertical"

       android:layout_marginStart="26dp"

       android:includeFontPadding="false"

       android:singleLine="true"

       android:text="@string/vx_change"

       android:textColor="@color/color_2d7eff"

       android:textSize="@dimen/common_title_size12"

       app:layout_constraintHorizontal_bias="0.0"

       app:layout_constraintBottom_toTopOf="@+id/guideline5"

       app:layout_constraintEnd_toStartOf="@+id/guideline3"

       app:layout_constraintStart_toEndOf="@+id/vx_content"

       app:layout_constraintTop_toTopOf="@+id/guideline4" />

       android:id="@+id/guideline5"

       android:layout_width="wrap_content"

       android:layout_height="wrap_content"

       android:orientation="horizontal"

       app:layout_constraintGuide_begin="74dp" />

       android:id="@+id/seven_iv"

       android:layout_width="wrap_content"

       android:layout_height="wrap_content"

       android:contentDescription="TODO"

       android:paddingTop="@dimen/common_size_5"

       android:paddingBottom="@dimen/common_size_5"

       android:src="@drawable/nochoice_squareicon"

       app:layout_constraintStart_toStartOf="@+id/guideline2"

       app:layout_constraintTop_toTopOf="@+id/guideline7" />

       android:id="@+id/seven_tv"

       android:layout_width="wrap_content"

       android:layout_height="wrap_content"

       android:includeFontPadding="false"

       android:paddingStart="8dp"

       android:paddingTop="@dimen/common_size_5"

       android:paddingBottom="@dimen/common_size_5"

       android:text="@string/seven_day_not_notice"

       android:textColor="@color/grey_999999"

       android:textSize="12sp"

       app:layout_constraintStart_toEndOf="@+id/seven_iv"

       app:layout_constraintTop_toTopOf="@+id/guideline7" />

       android:id="@+id/group"

       android:layout_width="wrap_content"

       android:layout_height="wrap_content"

       app:constraint_referenced_ids="seven_tv,seven_iv" />

       android:id="@+id/guideline7"

       android:layout_width="wrap_content"

       android:layout_height="wrap_content"

       android:orientation="horizontal"

       app:layout_constraintGuide_begin="97dp" />

       android:id="@+id/guideline15"

       android:layout_width="wrap_content"

       android:layout_height="wrap_content"

       android:orientation="horizontal"

       app:layout_constraintGuide_begin="113dp" />

       android:id="@+id/guideline16"

       android:layout_width="wrap_content"

       android:layout_height="wrap_content"

       android:orientation="horizontal"

       app:layout_constraintGuide_begin="136dp" />

</android.support.constraint.ConstraintLayout>

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 162,825评论 4 377
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 68,887评论 2 308
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 112,425评论 0 255
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,801评论 0 224
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 53,252评论 3 299
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 41,089评论 1 226
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 32,216评论 2 322
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 31,005评论 0 215
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,747评论 1 250
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,883评论 2 255
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,354评论 1 265
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,694评论 3 265
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,406评论 3 246
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,222评论 0 9
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,996评论 0 201
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 36,242评论 2 287
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 36,017评论 2 281

推荐阅读更多精彩内容