Java Android上的箭头文本视图形状
textview必须以箭头结尾,在第二个示例中应以箭头开头。请看下面的图片 如何在android java和eclipse上实现这一点Java Android上的箭头文本视图形状,java,android,eclipse,Java,Android,Eclipse,textview必须以箭头结尾,在第二个示例中应以箭头开头。请看下面的图片 如何在android java和eclipse上实现这一点 我唯一能找到的方法是将红色的bgcolor赋予textview,并在图片(箭头)的末尾粘贴一张图片。制作一个可绘制的/arrow\u shape.xml并使用以下代码: <?xml version="1.0" encoding="UTF-8"?> <layer-list xmlns:android="http://schemas.androi
我唯一能找到的方法是将红色的bgcolor赋予textview,并在图片(箭头)的末尾粘贴一张图片。制作一个可绘制的/arrow\u shape.xml并使用以下代码:
<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape android:shape="rectangle">
<size
android:width="100dp"
android:height="40dp" />
<solid android:color="#5EB888" />
<corners android:radius="0dp"/>
</shape>
</item>
<item
android:top="-40dp"
android:bottom="65dp"
android:left="-30dp"
android:right="-10dp">
<rotate
android:fromDegrees="135">
<shape android:shape="rectangle">
<solid android:color="#ffffff" />
</shape>
</rotate>
</item>
<item
android:top="-10dp"
android:bottom="0dp"
android:left="65dp"
android:right="-15dp">
<rotate
android:fromDegrees="130">
<shape android:shape="rectangle">
<solid android:color="#ffffff" />
<corners
android:radius="1dp"
android:bottomLeftRadius="0dp"/>
</shape>
</rotate>
</item>
<item
android:top="10dp"
android:width="-50dp"
android:bottom="5dp"
android:left="60dp"
android:right="-5dp">
<rotate
android:fromDegrees="230">
<shape android:shape="rectangle">
<solid android:color="#ffffff" />
</shape>
</rotate>
</item>
<item
android:top="65dp"
android:bottom="-40dp"
android:left="-22dp"
android:right="-20dp">
<rotate
android:fromDegrees="-320">
<shape android:shape="rectangle">
<solid android:color="#ffffff" />
</shape>
</rotate>
</item>
</layer-list>
然后在任何TextView
上使用它,比如android:background=“@drawable/arrow\u shape”
,结果是:
您还可以选中此项,并在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。
这就是我所拥有的:
您可以检查此回购以获得此箭头形状
//您的子布局
谢谢你的努力,这不是我想要的。因为必须涂成白色才能显示箭头…listview上使用的元素,在lstv_row.xml中有详细说明,我忘了在帖子中提到,很抱歉,不认为OnDraw适合这种情况。。。谢谢你的时间。为什么你不想在listview中使用它?如果你没有在listview上尝试,onDraw会一直被调用。。!像每个视图一样,将在需要时调用
<com.example.arrowshape.shapes.BubbleView
android:layout_width="200dp"
android:layout_marginTop="20dp"
app:shape_bubble_borderRadius="0dp"
app:shape_bubble_arrowPosition="left"
app:shape_bubble_arrowHeight="60dp"
app:shape_bubble_arrowWidth="100dp"
android:layout_height="100dp">
// your child layout
</com.example.arrowshape.shapes.BubbleView>