2016-08-17 4 views
1

Android에서 버튼으로 사용할 수있는 이등변 삼각형을 만들 수있는 방법이 있습니까?Android에서 삼각형 버튼 만들기

이 버튼은 전체 화면을 차지 : I 버튼처럼 보이는 삼각형을 만들 수 쉐이프를 사용하여 시도했다

<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> 

:

여기 enter image description here

내 현재 XML입니다 필요하지만 ShapeDrawable이 작동하지 않고 ImageButton을 만들려고했지만 버튼이 전체 화면을 차지하고 일반 버튼을 사용했지만 버튼이 여전히 전체 화면을 차지하고 ImageView를 사용하려고 시도했지만 여전히 똑같은 일을합니다.

버튼 크기가 너무 크고 실제 버튼 크기를 줄이면 이미지 자체가 축소되어보기 나 버튼 중 하나를 채우기 위해 이미지를 늘리는 방법을 찾을 수 없습니다.

미리보기 옵션이 매우 편리하기 때문에 Android Studio에서이 기능을 구현하려고했지만 Xamarin에서 사용할 수있는 무언가가 있거나 프로그래밍 방식으로 할 수있는 것이 있다면 열어 두었습니다.

답변

0

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


} 
} 

을 필요한 수 있습니다 | 두 개의 직사각형이 겹쳐져 있습니다. 값을 많이 사용해야하기 때문에 여러보기에서 사용하기가 어렵습니다. 이 경우에는 사용자 지정보기를 사용하는 것이 가장 좋은 방법이라고 생각합니다.

0

당신은 화면 높이를 측정하고 버튼이 얼마나 많은 (예 : 퍼센트로) 표시되는지 알고 있어야합니다. 하지만 여기서 아주 나쁜 방향으로 가고 있습니다. 샘플 : 풀 HD 디스플레이가있는 두 개의 장치가 있으며 실제 버튼은 집/스위치/백이 있고 화면에 두 번째입니다 - 비트 맵이 그 중 하나에 뻗어 있으므로 버튼을 올바르게 늘려야합니다. 이 방법을

편집을하지 않을 경우에는 ... 수직으로 뻗어 배경에 관계없이 ImageView 그래서 "고정"할 것이다 높이 wrap_content 세트를 가지고 : 당신은 버튼의 "비율"크기를 계산하는있는 LinearLayout의 weightSum의 PARAM을 시도 할 수 있습니다. 하지 너무 잘하지만, 당신이 XML 예제에 대해서는

<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> 

관련 문제