更改TextView样式的Android应用程序



如何将android应用程序中的TextView样式更改为我喜欢的任何设计?

例如在消息传递中:在气球中显示消息(如iPhone inBox)。

谢谢,

您可以更改XML中文本视图的背景属性,也可以通过编程方式更改。使用9补丁工具创建背景图像。因此,图像拉伸不会有任何问题。

还有一个选项是在资源文件夹中创建一个XML,如下所示,您可以在其中对Image 进行大量更改(Gradients、Corners、padding等)

    <?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
    <gradient android:startColor="#FF00FF" android:endColor="#FFFF00"
            android:angle="270"/>
    <solid android:color="#00000000"/>
    <stroke android:width="1dp" android:color="@color/round_rect_shape_border"/>
     <padding android:left="7dp" android:top="7dp"
            android:right="7dp" android:bottom="7dp" />
    <corners android:radius="8dp" />
</shape>

将其用作Textview的背景。

回答您的问题不仅适用于textview,而且适用于android中的其他view

您将需要一个新的self-class来扩展您需要更改样式的视图。

例如:

public class NewTextView extends TextView{
  public NewTextView(){} //just constructor
  @Override
  public void onDraw(Canvas canvas){
  //this is a main method that do your work.
  //for example, you will draw a `baloon` like iPhone
  }

这里有一个示例代码,它在EditText的每一行中画一条直线(就像你在纸上打字一样)。你可以看到这个代码并学习如何像它一样做。

再次强调:要做到这一点,你应该了解一些在android(Canvas或OpenGL)中绘图的知识。

public class EditTextExtra extends EditText {
    private Rect Rect;
    private Paint Paint;
    public EditTextExtra (Context context, AttributeSet attrs) {
        super(context, attrs);
        Rect = new Rect();
        Paint = new Paint();
        Paint.setStyle(Paint.Style.FILL_AND_STROKE);
        Paint.setColor(Color.BLACK);       
    }
    @Override
    protected void onDraw(Canvas canvas) {
        int count = getHeight()/getLineHeight();
        if(getLineCount() > count){
            count = getLineCount();   // for long text with scrolling
        }
        Rect r = Rect;
        Paint paint = Paint;
        int baseline = getLineBounds(0, r); // first line
        for (int i = 0; i < count; i++) {
            canvas.drawLine(r.left, baseline + 1, r.right, baseline + 1, paint);
            baseline += getLineHeight(); // next line
        }
        super.onDraw(canvas);
    }
}

相关内容

最新更新