如果文本太长,Android Textview会剪掉部分字母

时间:2017-01-09 08:55:26

标签: android textview xamarin.android

我想让TextView看起来像这样

Two TextViews, one under another

其中黑色部分是TextView,宽度小于灰色部分。这是某种进步条。有趣的是,这种方法适用于6.0.1,它适用于5.1.1,没有任何特殊偏好。但它在Lollipop上停止工作而代码没有任何变化,现在TextView只显示完整的字母,而不会切割到部分(例如,在这种情况下,黑色文本只会是“或”)

编辑:

布局代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="8dp"
android:paddingLeft="8dp">
<TextView
    android:text="Text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/title"
    android:textColor="@color/grey"
    android:textSize="15sp"
    android:textStyle="bold"
    android:lines="1"
    android:maxLines="1" />
<TextView
    android:text="Text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/titleselected"
    android:textStyle="bold"
    android:textSize="15sp"
    android:textColor="@android:color/black"
    android:lines="1"
    android:maxLines="1" />
</RelativeLayout>

改变进度:

titleSelected.LayoutParameters = new RelativeLayout.LayoutParams(Width, ViewGroup.LayoutParams.WrapContent);

2 个答案:

答案 0 :(得分:0)

您可以使用此示例代码并根据您的要求进行修改

来实现此目的
import android.app.Activity;
import android.graphics.Canvas;
import android.graphics.Picture;
import android.graphics.Rect;
import android.graphics.Region;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.PictureDrawable;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextPaint;
import android.text.style.DynamicDrawableSpan;
import android.text.style.ForegroundColorSpan;
import android.widget.TextView;

public class Sample extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.acitivity_sample);
    TextView tv = (TextView) findViewById(R.id.textView1);
    Spannable text = new SpannableStringBuilder("Loading..");
    setSpan(tv, text);
    tv.setText(text);
}

private void setSpan(TextView tv, Spannable text) {
    int blueColor = 0xff0000ff;
    int redColor = 0xffff0000;
    ForegroundColorSpan blue = new ForegroundColorSpan(blueColor);
    ForegroundColorSpan red = new ForegroundColorSpan(redColor);
    HalfColorApply o = new HalfColorApply("o", tv, blueColor, redColor);
    text.setSpan(blue, 0, 1, 0);
    text.setSpan(o, 1, 2, 0);
    text.setSpan(red, 3, text.length(), 0);
}

class HalfColorApply extends DynamicDrawableSpan {
    private final static String TAG = "DrawableSpanTest";
    Picture mPicture;

    public HalfColorApply(String text, TextView tv, int c0, int c1) {
        super(ALIGN_BASELINE);
        mPicture = new Picture();
        TextPaint p = tv.getPaint();
        Rect bounds = new Rect();
        p.getTextBounds(text, 0, text.length(), bounds);
        float width = p.measureText(text);
        float height = bounds.height();
        float y = height;
        Canvas c = mPicture.beginRecording((int) width, (int) height);
        c.save(Canvas.CLIP_SAVE_FLAG);
        // c.drawColor(0x[masked]);
        p = new TextPaint(p);
        p.setColor(c0);
        c.clipRect(0, 0, width / 2, height, Region.Op.REPLACE);
        c.drawText(text, 0, y, p);
        p.setColor(c1);
        c.clipRect(width / 2, 0, width, height, Region.Op.REPLACE);
        c.drawText(text, 0, y, p);
        c.restore();
        mPicture.endRecording();
    }

    @Override
    public Drawable getDrawable() {
        PictureDrawable d = new PictureDrawable(mPicture);
        d.setBounds(0, 0, mPicture.getWidth(), mPicture.getHeight());
        return d;
    }
}
}

答案 1 :(得分:0)

  

其中黑色部分是TextView,宽度小于灰色部分。这是某种进步条。

您可以更改layout_width TextView的{​​{1}},而不是更改right TextView,如下所示:

ClipBounds

和Main.axml:

[Activity(Label = "ChangingText", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
{
    Button btnClick;
    TextView titleselected;
    Rect drawingRect;
    int width;
    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);

        SetContentView (Resource.Layout.Main);
        btnClick = FindViewById<Button>(Resource.Id.btnClick);
        titleselected = FindViewById<TextView>(Resource.Id.titleselected);
        width = 1;
        btnClick.Click += BtnClick_Click;
    }

    private void BtnClick_Click(object sender, System.EventArgs e)
    {
        if (drawingRect == null) {
            drawingRect = new Rect();
            titleselected.GetDrawingRect(drawingRect);
        }
        titleselected.ClipBounds = new Rect(0, 0, width++, drawingRect.Bottom);
    }
}

效果很好。