【问题标题】:left drawable alignment with text in android与android中的文本左绘制对齐
【发布时间】:2016-06-25 01:32:37
【问题描述】:

我是自定义视图的新手,对 Android 中的画布了解不多, 我想将左侧可绘制对象与布局的右侧与此图像中相同 textview 的文本(无论是否多行)对齐

在上图中,我想将 $ 图像与文本($20.0 -$90.0 / 小时)对齐,并且如果文本是多行的并且我不想使用任何额外的布局,则必须居中。提前致谢

这是我的基本 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>

我曾尝试使用自定义视图来实现它,它适用于小文本,但是当文本变大时,它变得很奇怪,我的代码图像如下,

【问题讨论】:

  • @inkedTechie,我刚刚创建了不自定义的简单布局,因为我不知道从哪里开始。检查我编辑的问题
  • @VikramSingh 我不明白你为什么要使用 leftdrawable 属性而不是独立的 imageview 文本视图。
  • @misho,如果我有可用的属性,那我为什么要添加另一个视图。
  • @VikramSingh 因为这要容易得多,而且绝对没有理由不这样做
  • @misho,亲爱的朋友,我想使用自定义视图,我已经为小文本实现了它,但对于大文本感到很奇怪......无论如何,谢谢队友。

标签: android android-edittext android-canvas android-view textview


【解决方案1】:

试试这样的

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);
}
}

【讨论】:

  • 请在canvas.translate后添加右边距,等于平移距离
【解决方案2】:
<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>

【讨论】:

  • 你添加的风格是什么?
  • 我希望第二个 textview 与左 drawable 右对齐而不是这个 ....谢谢
  • 仍然无法正常工作,因为左侧可绘制对象在左侧,文本在右侧,两者之间的距离太远
  • 你确定吗?再检查一遍
猜你喜欢
  • 2011-12-31
  • 1970-01-01
  • 2014-12-12
  • 1970-01-01
  • 2015-02-12
  • 1970-01-01
  • 2012-02-24
  • 2018-05-12
相关资源
最近更新 更多