在Android中制作三角形按钮
有没有办法制作一个等腰三角形,我可以用它作为安卓系统的按钮 此按钮占据整个屏幕: 以下是我当前的xml:在Android中制作三角形按钮,android,xml,xamarin,Android,Xml,Xamarin,有没有办法制作一个等腰三角形,我可以用它作为安卓系统的按钮 此按钮占据整个屏幕: 以下是我当前的xml: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/main_menu_bg"
>
<RelativeLayout
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/ai_button"
android:background="@drawable/ai_button"
android:layout_alignParentLeft="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
</RelativeLayout>
我试着用ShapeDrawable创建一个看起来像我需要的按钮的三角形,但是ShapeDrawable不起作用,我试着制作一个ImageButton,但是按钮占据了整个屏幕,我试着用一个普通的按钮,但是按钮仍然占据了整个屏幕,我试着用ImageView,但是它仍然做同样的事情
按钮的大小总是太大,如果我缩小实际的按钮大小,那么图像本身会在内部收缩,我无法找到一种方法来拉伸图像以填充视图或按钮
我一直在尝试在Android Studio中构建它,因为预览选项非常方便,但是如果我可以在Xamarin中使用某些东西,或者如果我可以通过编程方式执行某些事情,我会打开它。您必须测量屏幕高度,并知道您的按钮将覆盖多少(例如百分比)。但是你来这里的时候真是太糟糕了。示例:有两个设备具有全高清显示,一个带有物理按钮home/switch/back,另一个带有屏幕上的按钮-您的位图将显示在其中一个设备上,因此您还应该适当地拉伸按钮。。。并且您为您的
ImageView
设置了wrap\u内容
,因此无论垂直拉伸背景如何,它的高度都是“固定的”。。。不要走这条路
编辑:您可以尝试LinearLayout的weightSum参数来计算按钮的“百分比”大小。不太好,但可能是你需要的
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="3"
android:gravity="center"
android:src="@drawable/main_menu_bg"
>
<ImageView
android:id="@+id/ai_button"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:src="@drawable/ai_button"
android:scaleType="fitXY"/>
</RelativeLayout>
xml
关于xml示例|有两个重叠的矩形。您必须经常处理这些值,这使得在不同的视图上使用它们变得困难。我认为在这种情况下,使用自定义视图是最好的解决方案
<item android:top="45dp">
<shape>
<size android:height="100dp" android:width="90dp"/>
<solid android:color="@android:color/holo_orange_light" />
</shape>
</item>
<item android:top="36dp" android:left="11dp">
<rotate
android:fromDegrees="45"
android:toDegrees="0"
android:pivotX="80%"
android:pivotY="20%" >
<shape>
<size android:width="40dp"
android:height="30dp"/>
<stroke android:color="@android:color/holo_orange_light" android:width="1dp"/>
<solid android:color="@android:color/holo_orange_light" />
</shape>
</rotate>
Use it in your layout
public class CustomTextView extends TextView {
private int mWidth;
private int mHeight;
public CustomTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint mPaint = new Paint();
int color = getResources().getColor(R.color.YourColor);
mPaint.setColor(color);
Path mPath = new Path();
mPath.moveTo(.0f, this.getHeight());
mPath.lineTo(0.8f * this.getWidth(), this.getHeight());
mPath.lineTo(this.getWidth(), 0.5f * this.getHeight());
mPath.lineTo(0.8f * this.getWidth(), .0f);
mPath.lineTo(.0f, .0f);
mPath.lineTo(.0f, this.getHeight());
canvas.clipPath(mPath);
canvas.drawPath(mPath,mPaint);
}
}