10

I want this kind of look. They don't seem like tabs so I believe they are buttons.

enter image description here

I am able to acheive the similar type with toggle button by keeping a selector in drawable.

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

But they have only two values and by clicking on the selected tab gets the other selected and the present one unselected. But I want exactly like tab. So can someone please help me achieve it? Thanks in advance.

4

3 回答 3

16

您必须为所有按钮创建选择器并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>
于 2013-07-09T06:37:47.630 回答
2

你可以做这样的事情,而不是切换按钮使用普通按钮。
如果单击“数据”按钮,请这样做

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

如果单击“H-chart”按钮

 H_chart(View v)
 {
  databtn.setBackgroundResource(R.drawable.image2);
  w_chartbtn.setBackgroundResource(R.drawable.image2);
  H_chartbtn.setBackgroundResource(R.drawable.image1);
 }
于 2013-07-09T04:57:34.260 回答
1

使用简单按钮而不是工具按钮。并设置这样的背景:

我以 1 个按钮为例:

 android:background="@drawable/data_button_select_state"

并在名为 data_button_select_state 的“drawable”文件夹中添加一个 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);

        }
    });

像这样更改其他按钮。这可能会帮助你...

于 2013-07-09T05:48:09.843 回答