Android开发

TextView设置drawableLeft后设置间距

2018-05-09  本文已影响153人  王怀智

问题复原

UI设计师给的图,图片文字在一行


image.png

怎么整

我直接使用的TextView的drawableLeft属性,直接把图片放置在文本前头。

        <TextView
            android:id="@+id/act_message_textview1"
            android:layout_width="match_parent"
            android:layout_height="43dp"
            android:gravity="center_vertical"
            android:textColor="@color/black"
            android:layout_marginLeft="10dp"
            android:drawableLeft="@mipmap/msg_link"
            android:text="@string/home2_chat_message"
            />

那么问题来了,设计师曰:"你这没有按设计稿走,图片与文字直接应该有间距...."

解决问题

当textview设置了drawableLeft之后,图片紧贴文本,如何想要预留出间距。需要借用drawblePadding属性

setCompoundDrawablePadding(4);

    TextView textView = new TextView(this);  
    textView.setText(MemDesc);  
    //在左侧添加图片  
    Drawable drawable= getResources().getDrawable(R.drawable.gray_circle);  
    drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());  
      
    textView.setCompoundDrawables(drawable, null, null, null);  
    textView.setTextColor(getResources().getColor(R.color.gray_textcolor_shen));  
    textView.setCompoundDrawablePadding(4);//设置图片和text之间的间距   

android:drawablePadding="@dimen/dp6"

       <TextView
            android:id="@+id/act_message_textview1"
            android:layout_width="match_parent"
            android:layout_height="43dp"
            android:gravity="center_vertical"
            android:textColor="@color/black"
            android:layout_marginLeft="10dp"
            android:drawableLeft="@mipmap/msg_link"
            <!--设置间距为6dp--> 
            android:drawablePadding="@dimen/dp6"
            android:text="@string/home2_chat_message"
            />
上一篇下一篇

猜你喜欢

热点阅读