java - Android 上的箭头 textview 形状

标签 java android eclipse

TextView 必须以箭头结尾,在第二个示例中应以箭头开头。请看下图 textview arrow shape

如何使用 eclipse 在 android java 上实现这一点?

我唯一能找到的方法是将红色的 bgcolor 赋予 textview,并在最后一张图片(箭头)处卡住

最佳答案

我认为最好以编程方式进行,例如:

package com.example.trist_000.alarm;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.widget.LinearLayout;
import android.widget.TextView;
public class customPrice extends LinearLayout {

TextView text1;
TextView text2;

public customPrice(Context context) {
    super(context);
}

public customPrice(Context context, AttributeSet attrs) {
    super(context, attrs);
    this.setWillNotDraw(false);
    this.setOrientation(HORIZONTAL);
    text1 = new TextView(context);
    text1.setText("2.00 $ base price");
    text1.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

    text2 = new TextView(context);
    text2.setText("1+1 offer");
    text2.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

    this.addView(text1);
    this.addView(text2);
}

Paint paint = new Paint();
Path path;

@Override
public void onDraw(Canvas canvas){
    int gap = 5;

    paint.setStyle(Paint.Style.FILL);
    paint.setStrokeWidth(2);
    paint.setColor(Color.RED);
    paint.setAntiAlias(true);
    path = new Path();
    path.moveTo(0, 0);
    path.lineTo(text1.getWidth(), 0);
    path.lineTo(text1.getWidth() - 5, text1.getHeight() / 2);
    path.lineTo(text1.getWidth(), text1.getHeight());
    path.lineTo(0, text1.getHeight());
    path.lineTo(0, 0);

    path.moveTo(text1.getWidth() + gap, 0);
    path.lineTo(text1.getWidth() + text2.getWidth() + gap, 0);
    path.lineTo(text1.getWidth()+text2.getWidth()+gap -5, text2.getHeight()/2);
    path.lineTo(text1.getWidth()+text2.getWidth()+gap, text2.getHeight());
    path.lineTo(text1.getWidth()+gap, text2.getHeight());
    path.lineTo(text1.getWidth()+gap-5, text2.getHeight()/2);

    path.close();
    canvas.drawPath(path, paint);
    super.onDraw(canvas);
}
}

在 xml 中我这样做了:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<com.example.trist_000.alarm.customPrice
    android:layout_marginLeft="3dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"></com.example.trist_000.alarm.customPrice>

它并不完美,需要在平局中更改一些内容,但我认为这是一个好的开始。可变差距是第一次平局和第二次平局之间的差异。 你必须用你自己的路径替换 com.example.trist_000.alarm。 这就是我所拥有的: Arrow

关于java - Android 上的箭头 textview 形状,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35521289/

相关文章:

java - JAR 中的 Android 库资源

java - Excel Apache POI 在执行 setCellValue() 时仅打印最后一个元素

Java/安卓 : Return a class name through a method

java - 为 CMake 设置自定义 Java 版本

Android-在启动期间隐藏操作栏然后再次显示?

java - 是否可以在 Firebase Firestore 文档中找到字段数。我如何在 Android Studio 中做到这一点

java - java中的@符号是什么意思?

java - Spring Boot 进度报告

Android Studio "Project Structure"无法正常运行

java - 在 x64 Windows 上使用 x32 JDK 在 x64 Eclipse 上针对 x32 服务器进行编译