TextView 必须以箭头结尾,在第二个示例中应以箭头开头。请看下图
如何使用 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。 这就是我所拥有的:
关于java - Android 上的箭头 textview 形状,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35521289/