温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

fragment和radiobutton做出类tabhost效果

发布时间:2020-06-17 12:30:47 来源:网络 阅读:752 作者:吃素只吃肉 栏目:移动开发

首先给两个截图看一下 基本框架,java和xml文件

每一个java对应一个Fragment,每一个Fragment对应一个xml文件

fragment和radiobutton做出类tabhost效果

fragment和radiobutton做出类tabhost效果

主要内容:


如何定制RadioButton:

设置radiobutton的android:button="@null";

设置radiobutton的android:background为一个自定义selector

设置alignparentbottom=true;



废话不多说了,上
                   .-' _..`.
                  /  .'_.'.'
                 | .' (.)`.
                 ;'   ,_   `.
 .--.__________.'    ;  `.;-'
|  ./               /
|  |               /
`..'`-._  _____, ..'
     / | |     | |\ \
    / /| |     | | \ \
   / / | |     | |  \ \
  /_/  |_|     |_|   \_\
 |__\  |__\    |__\  |__\

ZhuActivity  java代码

package net.xiaocool.yunzhu.activity;
/**
 * Created by Administrator on 2015/6/11.
 */
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.Window;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import net.xiaocool.yunzhu.R;
public class ZhuActivity extends FragmentActivity {
    private Fragment[] mFragments;
    private RadioGroup bottomRg;
    private FragmentManager fragmentManager;
    private FragmentTransaction fragmentTransaction;
    private RadioButton rbone, rbTwo, three, rbFour;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.textradiobutton);
        mFragments = new Fragment[4];
        fragmentManager = getSupportFragmentManager();
        mFragments[0] = fragmentManager.findFragmentById(R.id.fragement_dingdan);
        mFragments[1] = fragmentManager.findFragmentById(R.id.fragement_dingdan);
        mFragments[2] = fragmentManager.findFragmentById(R.id.fragement_chat);
        mFragments[3] = fragmentManager.findFragmentById(R.id.fragement_chaper);
        fragmentTransaction = fragmentManager.beginTransaction()
                .hide(mFragments[0]).hide(mFragments[1]).hide(mFragments[2]).hide(mFragments[3]);
        fragmentTransaction.show(mFragments[0]).commit();
        setFragmentIndicator();
    }

    private void setFragmentIndicator() {
        bottomRg = (RadioGroup) findViewById(R.id.group);
        rbone = (RadioButton) findViewById(R.id.one);
        rbTwo = (RadioButton) findViewById(R.id.two);
        three = (RadioButton) findViewById(R.id.three);
        rbFour = (RadioButton) findViewById(R.id.four);
        bottomRg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
        fragmentTransaction = fragmentManager.beginTransaction()
        .hide(mFragments[0]).hide(mFragments[1])
        .hide(mFragments[2]
        ).hide(mFragments[3]);
        switch (checkedId) {
        case R.id.one:
        fragmentTransaction.show(mFragments[0]).commit();
        break;
        case R.id.two:
        fragmentTransaction.show(mFragments[1]).commit();
        break;
        case R.id.three:
        fragmentTransaction.show(mFragments[2]).commit();
        break;
        case R.id.four:
        fragmentTransaction.show(mFragments[3]).commit();
        break;
default:
        break;
        }
        }
        });
        }
        }





对应的xml文件


<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"
tools:context=".MainActivity" >
    <!--<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:orientation="horizontal"
        android:background="#0061BB"
        >
        <ImageView
            android:id="@+id/me"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_marginLeft="15dp"
            android:src="@drawable/ic_login_phone"
            android:layout_gravity="center_vertical"
            />
        <TextView

            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#EAF2FA"
            android:text="云助养车"
            android:textSize="30sp"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="60dp"/>
    </LinearLayout>-->
    <fragment
        android:id="@+id/fragment_diandian"
        android:name="net.xiaocool.yunzhu.activity.Frag"

        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="10" />

    <fragment
        android:id="@+id/fragement_dingdan"
        android:name="net.xiaocool.yunzhu.activity.Frag2"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="10" />

    <fragment
        android:name="net.xiaocool.yunzhu.activity.Frag3"
        android:id="@+id/fragement_chat"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="10" />
    <fragment
        android:id="@+id/fragement_chaper"
        android:name="net.xiaocool.yunzhu.activity.Frag4"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="10" />

    <RadioGroup
    android:id="@+id/group"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:orientation="horizontal" >
    <RadioButton
        android:id="@+id/one"
        android:layout_width="96dp"
        android:layout_height="wrap_content"
        android:drawableTop="@drawable/change"
       
        android:background="@drawable/bg_button_down"

        android:button="@null"
        android:padding="10dip"
        android:text="Wechat"
        android:textSize="20sp" />
    <RadioButton
        android:id="@+id/two"
        android:layout_width="96dp"
        android:layout_height="wrap_content"
        android:background="@drawable/bg_button_down"
        android:button="@null"
        android:padding="10dip"
        android:text="Content"
        android:textSize="20sp" />
    <RadioButton
        android:id="@+id/three"
        android:layout_width="96dp"
        android:layout_height="wrap_content"
        android:background="@drawable/bg_button_down"
        android:button="@null"
        android:padding="10dip"
        android:text="Find"
        android:textSize="20sp"
        />
    <RadioButton
        android:id="@+id/four"
        android:layout_width="96dp"
        android:layout_height="wrap_content"
        android:background="@drawable/bg_button_down"
        android:button="@null"
        android:padding="10dip"
        android:text="Me"
        android:textSize="20sp" />
</RadioGroup>
<!--<android.support.v4.view.ViewPager
    android:id="@+id/content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@id/group"
    android:layout_alignParentTop="true" />-->
</RelativeLayout>




四个Frag

package net.xiaocool.yunzhu.activity;

import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import net.xiaocool.yunzhu.R;

/**
 * Created by Administrator on 2015/6/11.
 */
public class Frag extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.frag1, container, false);
    }
}

对应的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="vertical" >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:orientation="horizontal"
        android:background="#0061BB"
        >


        <TextView
            android:gravity="center"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:textColor="#EAF2FA"
            android:text="订单"
            android:textSize="30sp"

            />
    </LinearLayout>


    <TextView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center"
        android:text="这是订单"
        android:textColor="#000000" />

</LinearLayout>










































向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI