【问题标题】:Changing TextView Style Android applications更改 TextView 样式的 Android 应用程序
【发布时间】:2012-04-05 13:50:56
【问题描述】:

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

例如在消息传递中:在气球内显示消息(如在 iPhone 收件箱中)。

谢谢,

【问题讨论】:

  • 请不要让安卓应用看起来像 iOS 应用。

标签: java android xml styles message


【解决方案1】:

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

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

例如:

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

【讨论】:

  • 感谢您的精彩评论 :)
【解决方案2】:

您可以在 XML 中更改 textview 的背景属性,也可以通过编程方式执行此操作。使用 9-patch 工具创建背景图像。因此,图像拉伸不会有问题。

另一种选择是在资源文件夹中创建一个 XML,如下所示,您可以在其中对图像进行大量更改(渐变、角、填充等)

    <?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 的背景。

【讨论】:

  • 即使你可以选择以下选项 schemas.android.com/apk/res/android">
猜你喜欢
  • 2016-07-05
  • 1970-01-01
  • 1970-01-01
  • 2016-04-24
  • 2011-09-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多