Android TextView前加图标垂直居中第一行(仿大众点评购买须知/提示语)


如上图,需求在每条提示语句前加一个小圆点,我刚看到需求就想到用 android:drawableLeft 来作,可作完发现:当TextView内容为单行的时候是没有问题的,多行的时候,添加的这个drawableLeft就随着TextView高度而垂直居中了,变成下面的样子 java

看了下发现android:drawableLeft & android:drawableRight 是始终垂直居中于高度的 android

翻来翻去没有找到解决方法,就决定用简单粗暴的方法: 布局

布局: spa


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/act_item_experience_datail_tip_layout"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:background="@color/black"
              android:orientation="horizontal">
    <ImageView
        android:id="@+id/tip_img"
        android:layout_width="wrap_content"
        android:src="@mipmap/tip_left"
        android:layout_height="match_parent"/>
    <TextView
        android:id="@+id/tip_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="@color/gold"/>
</LinearLayout>


margin、lineSpacingExtra这些随喜爱设置



LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            lp.setMargins(0, (int)(mTipText.getLineHeight()*0.5), 0, 0);
            mTipImageView.setLayoutParams(lp);

经过mTipText.getLineHeight() 来得到TextView的行高,再设置行高的一半Margin就能获得下面的效果 code

(注:若是设置了Padding或者Margin,mTipText.getLineHeight()*0.5 这就不必定是行高的一半(0.5)了,慢慢调整,设置过Padding和Margin后,会大于0.5) xml

相关文章
相关标签/搜索