如上圖,需求在每條提示語句前加一個小圓點,我剛看到需求就想到用 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>
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