【问题标题】:Align drawable in textview在textview中对齐drawable
【发布时间】:2014-10-29 17:47:53
【问题描述】:

我的应用中有一个文本视图,其中 android:layout_width 设置为 fill_parent,android:gravity 为“中心”。现在我想在textview的文本旁边有drawable。这可能吗?

【问题讨论】:

    标签: android textview


    【解决方案1】:

    使用这些来设置文本视图旁边的可绘制对象,具体取决于所需的位置。

    【讨论】:

    • 这些不起作用,因为它们会导致如下结果:link
    • 然后将其放入具有 match_parent 宽度的 LinearLayout 中,并将 TextView 的宽度设置为 wrap_content
    【解决方案2】:

    单独使用TextView 无法实现您想要的效果。

    一种选择是使用自定义布局。您可以查看HorizontalTailLayout。很容易在你的项目中获取和投入,这应该可以工作。

    另一个不完美的选择是使用RelativeLayout 和android:gravity="fill"。这将确保ImageView 始终紧贴TextView 的尾部。但缺点是即使使用其他布局包裹也无法使内容居中,因为使用android:layout_alignParentRight="true" 会将RelativeLayout 一直拉伸到右侧。

    例子:

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="fill">
    
        <ImageView
            android:id="@+id/iv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_alignParentRight="true"
            android:src="@android:drawable/ic_lock_lock"/>
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toLeftOf="@+id/iv"
            android:layout_centerVertical="true"
            android:singleLine="true"
            android:ellipsize="end"
            android:text="Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! "/>
    
    </RelativeLayout>
    

    【讨论】:

      猜你喜欢
      • 2014-11-05
      • 2014-06-06
      • 1970-01-01
      • 2011-09-16
      • 1970-01-01
      • 1970-01-01
      • 2014-02-06
      • 1970-01-01
      相关资源
      最近更新 更多