Android 实现TextView后面跟随一个高度和宽度固定的ImageView

安卓实现TextView后面跟随一个高度和宽度固定的ImageView(不要问我为什么不直接用drawableRight,因为宽高不好控制)
TextView只为1行,即singleLine为true,宽度不定,要求TextView和ImageView站在同一排。


如图:
TextView不满一行
TextView超出一行


直接上代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="16dp"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="
        安卓实现TextView后面跟随一个高度和宽度固定的ImageView(不要问我为什么不直接用drawableRight,因为宽高不好控制)\n
        TextView只为1行,即singleLine为true,宽度不定,要求TextView和ImageView站在同一排。" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:singleLine="true"
            android:text="1234567890123456789012345678901234567890123456789012345678901234567890" />

        <ImageView
            android:layout_width="16dp"
            android:layout_height="16dp"
            android:src="@drawable/ic_global_list_lable_groupon" />
    </LinearLayout>
</LinearLayout>

Linearlayout里面的weight其实很好用,但是有朋友可能会想着,为什么只给TextView加weight属性,为什么不给ImageView加。weight是权重的意思,给组件加了该属性之后,该组件就会与其他的同级组件比较权重,然后再布局。闲的无聊的可以去试试把ImageView加weight,或者把TextView的weight取消试试效果。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值