问题描述
我想要这种外观.它们看起来不像 tabs
所以我相信它们是按钮.
I want this kind of look. They don't seem like tabs
so I believe they are buttons.
通过将选择器保持在可绘制状态,我可以使用 toggle button
实现类似的类型.
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.
推荐答案
您必须为所有按钮创建选择器,并使用带有选择器背景和空按钮的 RadioGroup
..
You have to create selector for all button and use RadioGroup
with selector background and null button..
按照@Andru 回答创建选择器..
Follow @Andru answer for create Selector..
下面是 RadioGroup 代码.
Below is RadioGroup code.
<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>
这篇关于在android中制作看起来像标签的按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!