In my app I have a header bar which consists of a single textview with fill_parent as width, which have a specific background color and some centered text. Now I want to add a d
> This line is important ->> android:gravity="start|center_vertical
<LinearLayout
android:layout_width="match_parent"
android:layout_margin="@dimen/marginplus2"
android:layout_height="wrap_content">
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:text="@string/like"
android:gravity="start|center_vertical"
android:drawablePadding="@dimen/marginplus1"
android:drawableLeft="@drawable/ic_like"
android:layout_height="wrap_content" />
<TextView
android:layout_width="0dp"
android:layout_weight="1.5"
android:text="@string/comments"
android:drawablePadding="@dimen/marginplus1"
android:gravity="start|center_vertical"
android:drawableLeft="@drawable/ic_comment"
android:layout_height="wrap_content" />
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:text="@string/share"
android:drawablePadding="@dimen/marginplus1"
android:gravity="start|center_vertical"
android:drawableLeft="@drawable/ic_share"
android:layout_height="wrap_content" />
</LinearLayout>
The simplest way to obtain that is:
<RelativeLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/head" >
<ImageView
android:id="@+id/cat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:onClick="onClick"
android:layout_marginTop="10dp"
android:layout_alignParentLeft="true"
android:src="@drawable/btn_back_d" />
<TextView
android:id="@+id/TvTitle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text=""
android:layout_centerHorizontal="true"
android:textColor="#000000"
android:textSize="20sp"/>
Where
Try flowing:
public class DrawableCenterTextView extends AppCompatTextView {
public DrawableCenterTextView(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
}
public DrawableCenterTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public DrawableCenterTextView(Context context) {
super(context);
}
@Override
protected void onDraw(Canvas canvas) {
// We want the icon and/or text grouped together and centered as a group.
// We need to accommodate any existing padding
final float buttonContentWidth = getWidth() - getPaddingLeft() - getPaddingRight();
float textWidth = 0f;
final Layout layout = getLayout();
if (layout != null) {
for (int i = 0; i < layout.getLineCount(); i++) {
textWidth = Math.max(textWidth, layout.getLineRight(i));
}
}
// Compute left drawable width, if any
Drawable[] drawables = getCompoundDrawables();
Drawable drawableLeft = drawables[0];
int drawableWidth = (drawableLeft != null) ? drawableLeft.getIntrinsicWidth() : 0;
// We only count the drawable padding if there is both an icon and text
int drawablePadding = ((textWidth > 0) && (drawableLeft != null)) ? getCompoundDrawablePadding() : 0;
// Adjust contents to center
float bodyWidth = textWidth + drawableWidth + drawablePadding;
int translate = (int) ((buttonContentWidth - bodyWidth));
if (translate != 0)
setPadding(translate, 0, translate, 0);
super.onDraw(canvas);
}
}