溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

fragment和radiobutton做出類tabhost效果

發(fā)布時間:2020-06-17 12:30:47 來源:網絡 閱讀:751 作者:吃素只吃肉 欄目:移動開發(fā)

首先給兩個截圖看一下 基本框架,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="云助養(yǎng)車"
            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問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI