简体   繁体   中英

custom radio buttons to give functionality like tabhost

How to achieve the functionality of tabHost using radiobuttons in android ?

  • How to design a custom radiobuton group on the top,
  • so that on click on that button like we do it in tab host .... functionality of displaying the activity in obtained

tabs.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <RadioGroup
        android:id="@+id/radioGroup1"
        android:layout_width="fill_parent"
        android:layout_height="40dp"
        android:orientation="horizontal" >

        <RadioButton
            android:id="@+id/rdb1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="0.33"
            android:background="@android:color/white"
            android:button="@null"
            android:gravity="center"
            android:text="Tab1"
            android:textColor="@android:color/black"
            android:textSize="15sp"
            android:textStyle="bold" />

        <RadioButton
            android:id="@+id/rdb2"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="0.33"
            android:background="@android:color/white"
            android:button="@null"
            android:gravity="center"
            android:text="Tab2"
            android:textColor="@android:color/black"
            android:textSize="15sp"
            android:textStyle="bold" />


    </RadioGroup>

</LinearLayout>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <include layout="@layout/tabs"/>


</LinearLayout>

MainActivity.java

public class MainActivity extends Activity {

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

    }

}

screen1.java & screen2.java r other 2 activities

在此输入图像描述

Problem 1 how to create custom RadioButton ?

Answer: you can apply the custom style to your RedioButton. now there are very simple way to create custom style for a UI control. Here I show you how to crate custom style for RedioButton:

<style name="MyRadioButtonStyle" parent="@android:style/Widget.CompoundButton.RadioButton">
<item name="android:button">@drawable/custom_btn_radio</item>

create a drawble selector for the button state. res/drawable/custom_btn_radio.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:state_window_focused="false"
      android:drawable="@drawable/btn_radio_on" />
<item android:state_checked="false" android:state_window_focused="false"
      android:drawable="@drawable/btn_radio_off" />

<item android:state_checked="true" android:state_pressed="true"
      android:drawable="@drawable/btn_radio_on_pressed" />
<item android:state_checked="false" android:state_pressed="true"
      android:drawable="@drawable/btn_radio_off_pressed" />

<item android:state_checked="true" android:state_focused="true"
      android:drawable="@drawable/btn_radio_on_selected" />
<item android:state_checked="false" android:state_focused="true"
      android:drawable="@drawable/btn_radio_off_selected" />

<item android:state_checked="false" android:drawable="@drawable/btn_radio_off" />
<item android:state_checked="true" android:drawable="@drawable/btn_radio_on" />

Problem 2 How to show Two activity in a single Container(Perhaps It is also be a part of Activity) Answer : There are no way to open Activities in a single Activity class. You can open multiple Fragment in a single Activity. or Other way replace your layout at runtime on radio button selection. As i know First one is good way.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM