2013-07-09 36 views
9

我想要這種外觀。他們似乎不像tabs,所以我相信他們是按鈕。使按鈕看起來像在Android中的標籤

enter image description here

我能夠在繪製保持與選擇到toggle button的acheive相似類型。

<ToggleButton 
    android:id="@+id/toggleButton" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center_horizontal" 
    android:background="@drawable/custom_selector"  
    android:textOn=" Button1 Button2" 
    android:textOff=" Button1 Button2" /> 

但他們只有兩個值,並通過單擊選定的選項卡上獲取其他選擇本一條未選。但我想要完全像標籤。那麼有人可以幫我實現它嗎?提前致謝。

+0

不要切換按鈕使用,採取四個按鈕和使用選擇的繪製。 –

+0

@ManojPal謝謝,但那些看起來不像上面的角落形狀和所有。這也通過選擇一個按鈕之前選擇的按鈕應再次獲得正常的顏色。 – rick

+0

它的一點點棘手,你可以使用角形圖像作爲按鈕的背景,並添加一個條件,如果選擇了一個按鈕更改其他按鈕背景爲正常。它會給人感覺像標籤。 –

回答

13

你必須爲所有的按鈕來創建選擇和使用RadioGroup與選擇的背景和空鍵..

關注@Andru答案創建選擇..

下面是RadioGroup代碼。

<RadioGroup 
    android:id="@+id/rdogrp" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_margin="8dp" 
    android:gravity="center" 
    android:orientation="horizontal" > 

    <RadioButton 
     android:id="@+id/btn1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_margin="0dp" 
     android:background="@drawable/btn1Selector" 
     android:button="@null" 
     android:checked="true" 
     android:gravity="center" /> 

    <RadioButton 
     android:id="@+id/btn2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_margin="0dp" 
     android:background="@drawable/btn2Selector" 
     android:button="@null" 
     android:gravity="center" /> 

    <RadioButton 
     android:id="@+id/btn3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_margin="0dp" 
     android:background="@drawable/btn3Selector" 
     android:button="@null" 
     android:gravity="center" /> 

    <RadioButton 
     android:id="@+id/btn4" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_margin="0dp" 
     android:background="@drawable/btn4Selector" 
     android:button="@null" 
     android:gravity="center" /> 
</RadioGroup> 

這裏是示例代碼btn1Selector.xml

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:drawable="@drawable/btn1_selected" android:state_checked="true" /> 
    <item android:drawable="@drawable/btn1_normal" android:state_checked="false"/> 
</selector> 
+0

+1謝謝,會檢查它。 – rick

1

你可以這樣做,而不是切換按鈕使用普通按鈕。
如果 「數據」 按鈕,點擊做這樣

data(View v) 
{ 
    databtn.setBackgroundResource(R.drawable.image1); 
    w_chartbtn.setBackgroundResource(R.drawable.image2); 
    H_chartbtn.setBackgroundResource(R.drawable.image2); 
} 

如果 「H-圖」 按鈕,點擊

H_chart(View v) 
{ 
    databtn.setBackgroundResource(R.drawable.image2); 
    w_chartbtn.setBackgroundResource(R.drawable.image2); 
    H_chartbtn.setBackgroundResource(R.drawable.image1); 
} 
1

使用簡單的按鈕代替的toogle按鈕。而像這樣設置背景:

我給例如1個按鈕:

android:background="@drawable/data_button_select_state" 

,並在你的「繪製」文件夾命名data_button_select_state添加一個XML文件:

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android" > 
<item android:drawable="@drawable/data_image_selected_state" 
     android:state_selected="true" /> 
<item android:drawable="@drawable/data_image_selected_state" 
     android:state_pressed="true" /> 
<item android:drawable="@drawable/data_image_without_selected" /> 
</selector> 

現在添加代碼java文件是這樣的:

當點擊數據按鈕時:

data_button.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      data_button.setActivated(true); 
      H_chart_button.setActivated(false); 
          w_chart_button.setActivated(false); 
          hc_chart_button.setActivated(false); 

     } 
    }); 

更改其他按鈕也是如此。這將可以幫助你......

+0

+1謝謝。將嘗試它。 – rick

相關問題