【安卓】布局中避免子View被顶出

产品需求:

布局中有两个View(或者多个),他们的宽度都不确定,那么当其中一个宽度过宽时,要保证另一个View的正常显示.

效果图:

正常情况下:

宽度过宽情况下:

 

 

思路:

1.LinearLayout 线性布局权重(可行,但需要额外嵌套一层):

给第一个控件设置宽度0,权重1,第二个控件宽度自适应:

结果:过宽状态下效果正常,但是没有过宽时第二个View在父布局最右边.

补充:在最外层再添加一层布局

2.RelativeLayout 相对布局,条件方式(不可行):

效果同线性布局.......

3.ConstraintLayout 约束布局,约束条件(完美运行):

以下是代码:

            <androidx.constraintlayout.widget.ConstraintLayout
                android:layout_width="match_parent"
                android:layout_height="@dimen/dp_40">

                <TextView
                    android:id="@+id/tv_1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    app:layout_constrainedWidth="true"
                    app:layout_constraintHorizontal_chainStyle="packed"
                    app:layout_constraintTop_toTopOf="parent"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintLeft_toLeftOf="parent"
                    app:layout_constraintRight_toLeftOf="@id/tv_2"
                    app:layout_constraintHorizontal_bias="0"
                    android:text="生化危机366666666666666666666"
                    android:textColor="#ffffff"
                    android:singleLine="true"
                    android:ellipsize="end" />

                <TextView
                    android:id="@+id/tv_2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="@dimen/dp_10"
                    app:layout_constraintTop_toTopOf="parent"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintLeft_toRightOf="@id/tv_1"
                    app:layout_constraintRight_toRightOf="parent"
                    app:layout_constraintHorizontal_bias="0"
                    android:text="生化危机3"
                    android:textColor="#ffffff"/>

            </androidx.constraintlayout.widget.ConstraintLayout>

 

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

WWGtest

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值