我是自定义视图的新手,并且对android中的canvas知之甚少, 我想将左侧绘图与布局右侧以及与此图像中相同的文本视图的文本(无论是否为多行)对齐
在上图中,我想将$ image与文本对齐($ 20.0 - $ 90.0 / hour),如果文本是多行的,我必须在中心,我不想使用任何额外的布局。提前致谢
这是我的基本xml,
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"">
<TextView
android:id="@+id/ad_post_tv_noDays"
style="@style/textView"
android:text="20 days"
android:layout_width="wrap_content"
android:layout_alignParentLeft="true"
android:drawablePadding="5dp"
android:textColor="@color/color60"
android:drawableLeft="@drawable/icon_calender"
android:layout_gravity="left|center"
android:gravity="left|center"
android:layout_centerVertical="true"
/>
<TextView
style="@style/textView"
android:layout_alignParentRight="true"
android:id="@+id/ad_post_tv_wage"
android:layout_marginLeft="5dp"
android:text="$8 - $12/hour"
android:textSize="10dp"
android:layout_toRightOf="@id/ad_post_tv_noDays"
android:drawablePadding="5dp"
android:layout_width="wrap_content"
android:textStyle="bold"
android:layout_gravity="right|center"
android:gravity="right|center"
android:drawableLeft="@drawable/icon_dollar"
/>
</RelativeLayout>
答案 0 :(得分:2)
尝试这样的事情
package views;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.widget.TextView;
import com.upsilon.docta.R;
public class MyTempView extends TextView {
public MyTempView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyTempView(Context context) {
super(context);
}
@Override
protected void onDraw(Canvas canvas) {
//ContextCompat.getColor(mContext, R.color.back_text_color)
Paint textPaint = getPaint();
Rect bounds = new Rect();
textPaint.getTextBounds(getText().toString(), 0, getText().length(), bounds);
int textWidth = bounds.width();
Resources res = getResources();
Bitmap bitmap = BitmapFactory.decodeResource(res, R.drawable.ic_close);
canvas.drawBitmap(bitmap, 0, getHeight() / 2 - bitmap.getHeight() / 2, textPaint);
canvas.translate(bitmap.getWidth(), 0);
super.onDraw(canvas);
}
}
答案 1 :(得分:1)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/ad_post_tv_noDays"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:drawableLeft="@drawable/ic_cart_final"
android:drawablePadding="5dp"
android:gravity="center_vertical"
android:text="20 days"
android:textColor="@color/blue_btn_bg_color" />
<TextView
android:id="@+id/ad_post_tv_wage"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:drawableLeft="@drawable/ic_cart_final"
android:drawablePadding="5dp"
android:layout_gravity="right"
android:gravity="center_vertical|right"
android:text="$8 - $12/hour"
android:textSize="10dp"
android:textStyle="bold" /></LinearLayout>