2016-08-17 7 views
1

Androidでボタンとして使用できる二等辺三角形を作る方法はありますか?Androidで三角ボタンを作成する

このボタンは、画面全体を占める:ここ

enter image description here

は私の現在のxmlです:

<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を作成しようとしましたが、ボタンが画面全体を占めていましたが、通常のボタンを使ってみましたが、ボタンはまだ画面全体を占めていました。まだ同じことをする。

ボタンのサイズは常に大きすぎます。実際のボタンのサイズを縮小すると、画像自体が縮小し、画像を拡大してビューまたはボタンのいずれかを塗りつぶす方法がわかりません。

私はこれを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); 


} 
} 

を必要とするものであるかもしれません|重なり合う2つの長方形があります。あなたは値を多く使って遊ぶ必要があり、これは異なるビューでの使用を難しくします。この場合、カスタムビューを使用するのが最適なソリューションだと思います。

0

あなたは画面の高さを測定し、ボタンがどれくらい(例えばパーセントで)カバーするかを知る必要があります。しかし、あなたはここに非常に悪いアプローチをしています。サンプル:フルHD表示の2つのデバイスがあり、1つは物理的なボタンのホーム/スイッチ/バック、もう1つはオンスクリーンです。ビットマップはその1つに表示されるため、適切にボタンを伸ばす必要があります。あなたの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> 

関連する問題