2014-11-02 53 views
0

我有一個ImageButton,並在android:background屬性我目前有一個xml可繪製的按下時更改ImageButton背景顏色。這很好,但我也想爲這些ImageButton的每一個添加頂部邊框。如何將單邊框和背景添加到一個drawable中?

下面是我創建的示例圖片,以便更好地理解我的觀點。 這些按鈕也會有一個活動狀態,表示當前活動按鈕,我可以使用Java代碼將其設置爲可繪製的。

enter image description here

回答

1

您可以使用多個drawable來完成您的工作。您可以使用頂部邊框繪製可繪製的圖標/圖像,而不使用頂部邊框,並使用setBackgroundResource方法切換圖像背景。 (我相信你想顯示帶有頂部邊框的圖像作爲當前選擇的工具圖標,對不對?)。

正如你要構建像工具箱這樣的幾個圖像按鈕,你必須確保他們的選擇狀態得到適當的控制。如果選擇了一個圖像按鈕,其他所有人都應顯示未選中的繪圖。

我把一些代碼扔在一起,並建立這個小例子。希望你會發現它有用。

public class MainActivity extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     final ImageButton imButton1 = (ImageButton) findViewById(R.id.imButton1); 
     final ImageButton imButton2 = (ImageButton) findViewById(R.id.imButton2); 
     final ImageButton imButton3 = (ImageButton) findViewById(R.id.imButton3); 

     imButton1.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) 
      { 
       imButton1.setBackgroundResource(R.drawable.icon1_selected); 
       imButton2.setBackgroundResource(R.drawable.icon2_unselected); 
       imButton3.setBackgroundResource(R.drawable.icon3_unselected); 
      } 
     }); 

     imButton2.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) 
      { 
       imButton1.setBackgroundResource(R.drawable.icon1_unselected); 
       imButton2.setBackgroundResource(R.drawable.icon2_selected); 
       imButton3.setBackgroundResource(R.drawable.icon3_unselected); 
      } 
     }); 

     imButton3.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) 
      { 
       imButton1.setBackgroundResource(R.drawable.icon1_unselected); 
       imButton2.setBackgroundResource(R.drawable.icon2_unselected); 
       imButton3.setBackgroundResource(R.drawable.icon3_selected); 
      } 
     }); 
    } 
} 

,這是它的佈局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#bbb" 
    tools:context="${packageName}.${activityClass}" > 

    <ImageButton 
     android:id="@+id/imButton1" 
     android:layout_toLeftOf="@+id/imButton2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginRight="0dp" 
     android:layout_alignTop="@+id/imButton2" 
     android:background="@drawable/icon1_unselected" /> 

    <ImageButton 
     android:id="@id/imButton2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="23dp" 
     android:layout_alignParentTop="true" 
     android:background="@drawable/icon2_selected" /> 

    <ImageButton 
     android:id="@+id/imButton3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignTop="@id/imButton2" 
     android:layout_toRightOf="@id/imButton2" 
     android:background="@drawable/icon3_unselected" /> 

</RelativeLayout> 

工作圖像按鈕的屏幕截圖。

enter image description here enter image description here enter image description here

希望這有助於。

0

我通過創建兩個單獨繪製的XML文件,一個具有頂部邊框和白色背景,另一個有一點灰色背景相同顏色的邊框解決了這個問題。然後在selector xml文件中,我所要做的就是將這些xml文件中的每一個分配給它們各自的狀態並解決問題。 :)

0

試試這個

<?xml version="1.0" encoding="utf-8"?> 
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" > 
    <item 
     android:left="-6dp" 
     android:right="-6dp" 
     android:bottom="-6dp"> 
     <shape> 
      <stroke 
       android:width="5dp" 
       android:color="#6c6c6c" /> 

      <solid android:color="#FFFFFF" /> 
     </shape> 
    </item> 

</layer-list> 
0

嘿,你可以使用視圖這樣的邊界,你把這個在您的圖像視圖的所有方面,那麼你可以看到,

<View 
     android:layout_width="match_parent" 
     android:layout_height="1dp" 
     android:background="#000" /> 

任何顏色你想用。謝謝