2015-02-12 49 views
0

我想創建一個基於ImageView自定義視圖,使一個小窗口,如下,其中繪製源是相機和背景是與記號標誌矩形:重力XML繪製

Expected appearance of the widget

問題是:

  • 我無法在右側的刻度線標記上創建邊框:刻度線是全屏拉伸而不是停留在右下角。
  • 如果我使用android:background來設置刻度,那麼刻度符號會在圖像後面顯示。

這裏是我的xml文件:

目前的邊境XML繪製

<?xml version="1.0" encoding="utf-8"?> 
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> 
<item> 
    <shape android:shape="rectangle"> 
     <solid android:color="@android:color/transparent"/> 
     <stroke 
      android:width="2dip" 
      android:color="@color/bg_black"/> 
     <padding 
      android:left="3dp" 
      android:right="3dp" 
      android:top="3dp" 
      android:bottom="3dp" 
      /> 
    </shape> 
</item> 
<item > 
    <layer-list > 
     <item> 
      <shape 
       android:shape="rectangle"> 
       <stroke 
        android:width="2dp" 
        android:color="@color/border_yellow"/> 
       <size 
        android:width="37dp" 
        android:height="37dp"/> 
       <padding 
        android:bottom="3dp" 
        android:left="3dp" 
        android:right="3dp" 
        android:top="3dp"/> 
      </shape> 
     </item> 
     <item android:drawable="@drawable/ic_tick"/> 
    </layer-list> 
</item> 
</layer-list> 

圖片按鈕XML佈局

<ImageView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:src="@drawable/product1" 
    android:background="@drawable/toggle_rectangle_check"/> 

感謝 你的時間。

+0

你在哪裏使用這個繪製?發佈 – 2015-02-12 02:53:10

+0

@Rod_Algonquin你的意思是佈局xml。我發佈了它。謝謝你的時間。 – thuanle 2015-02-12 03:03:50

+0

我仍然不知道圖像背後的刻度線標記。因爲我想要它在頂部我們可以使用該功能的背景嗎?如果不是我們該如何實現這個功能:自定義'onDraw'函數? – thuanle 2015-02-12 03:07:47

回答

1

我不確定這是否是您要尋找的解決方案,但不是具有其中的樣式的xml文件,我相信最好有一個佈局文件來定義您的佈局爲您定製的Widget,像這樣

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:background="@drawable/customWidgetBox" 
     android:orientation="vertical"> 

     <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:background="@drawable/customCheckmark" 
     android:gravity="bottom|right" /> 

</LinearLayout> 

而到了繪製文件夾,你將需要添加兩個customCheckmark.xml和customWidgetBox.xml

customCheckmark.xml

<?xml version="1.0" encoding="UTF-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle"> 
    <item android:drawable="@drawable/check_mark"> 
     <stroke android:width="3dp" android:color="@color/blue_button_border" /> 
    </item> 
</shape> 

customWidgetBox.xml

<?xml version="1.0" encoding="UTF-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle"> 
    <item android:drawable="@drawable/camera"> 
     <stroke android:width="3dp" android:color="@color/blue_button_border" /> 
    </item> 
</shape> 

不知道如果這個代碼完成你想要什麼,但它會是一個很好的幫助,讓你開始,因爲這絕對是做到這一點! :)

有些鏈接可能爲例子很有意思:

Set border and background color of textView

Drawable Resource, Android API

how to put a border around an android textview

Tips & Tricks -XML Drawables Part 1

+0

我明白了你的想法。謝謝。但它不符合我的要求 – thuanle 2015-02-12 05:39:35