天天看點

Android 1對1直播源碼開發,底部導航欄的簡單實作

在Android 1對1直播源碼開發中,底部導航欄的簡單實作有兩種方法:

1、利用LinearLayout+TextView實作 1對1直播源碼中底部導航欄的效果。

2、利用RadioGroup+RadioButton實作 1對1直播源碼中底部導航欄的效果。

兩者的功能代碼,基本一緻,唯一的差別,也就是:TextView和RadioButton的差別。選擇樣式中的state_selected和state_checked的差別。

下面附上RadioGroup+RadioButton實作的功能代碼:

1、首先是 1對1直播源碼中底部導航欄點選效果的實作:

tab_menu_channel.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true" android:drawable="@mipmap/tab_channel_pressed"></item>
    <item android:drawable="@mipmap/tab_channel_normal"></item>
</selector>           

複制

其他三個,照着寫。

tab_menu_bg.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true" >
        <shape>
            <solid android:color="#FFC4C4C4"></solid>
        </shape>
    </item>
    <item>
        <shape>
            <solid android:color="@color/bg_white"></solid>
        </shape>
    </item>
</selector>           

複制

2、activity_main.xml布局代碼如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">
 
    <RelativeLayout
        android:id="@+id/ly_top_bar"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_alignParentTop="true">
 
        <TextView
            android:id="@+id/txt_topbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_centerInParent="true"
            android:gravity="center"
            android:textSize="18dp"
            android:textColor="@color/text_topbar"
            android:text="資訊"/>
 
 
        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="@color/div_white"
            android:layout_alignParentBottom="true"/>
 
    </RelativeLayout>
 
    <View
        android:id="@+id/div_tab_bar"
        android:layout_width="match_parent"
        android:layout_height="2px"
        android:background="@color/div_white"
        android:layout_above="@id/main_rgroupTabMenu"/>
 
    <RadioGroup
        android:id="@+id/main_rgroupTabMenu"
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:orientation="horizontal"
        android:background="@color/bg_white"
        android:layout_alignParentBottom="true">
 
        <RadioButton
            android:id="@+id/main_rbtnChannel"
            style="@style/TabMenuItem"
            android:drawableTop="@drawable/tab_menu_channel"
            android:text="提醒" />
 
        <RadioButton
            android:id="@+id/main_rbtnMessage"
            style="@style/TabMenuItem"
            android:drawableTop="@drawable/tab_menu_message"
            android:text="資訊" />
 
        <RadioButton
            android:id="@+id/main_rbtnMy"
            style="@style/TabMenuItem"
            android:drawableTop="@drawable/tab_menu_my"
            android:text="我的" />
 
        <RadioButton
            android:id="@+id/main_rbtnMore"
            style="@style/TabMenuItem"
            android:drawableTop="@drawable/tab_menu_better"
            android:text="更多" />
 
    </RadioGroup>
 
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/ly_top_bar"
        android:layout_above="@id/div_tab_bar"
        android:id="@+id/ly_content">
 
    </FrameLayout>
 
</RelativeLayout>           

複制

styles.xml的代碼如下:

<resources>
 
    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>
 
    <style name="TabMenuItem">
        <item name="android:layout_width">0dp</item>
        <item name="android:layout_height">match_parent</item>
        <item name="android:layout_weight">1</item>
        <item name="android:background">@drawable/tab_menu_bg</item>
        <item name="android:drawablePadding">3dp</item>
        <item name="android:gravity">center_horizontal</item>
        <item name="android:textSize">14dp</item>
        <item name="android:button">@null</item>
    </style>
 
</resources>           

複制

MainActivity.java的代碼如下:

package com.deepreality.fragmentcaseonedemo;
 
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;
 
public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {
 
    //UI Object
    private RadioButton rbtnChannel, rbtnMessage, rbtnMy, rbtnMore;
    private RadioGroup rgroupTabMenu;
 
    //Fragment Object
    private MyFragmentOne fg1;
    private MyFragmentTwo fg2;
    private MyFragmentThree fg3;
    private MyFragmentFour fg4;
    private FragmentManager fManager;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        fManager = getFragmentManager();
        bindViews();
 
        rbtnChannel.setChecked(true);
    }
 
    //UI元件初始化與事件綁定
    private void bindViews() {
        rgroupTabMenu = findViewById(R.id.main_rgroupTabMenu);
        rbtnChannel = findViewById(R.id.main_rbtnChannel);
        rbtnMessage = findViewById(R.id.main_rbtnMessage);
        rbtnMore = findViewById(R.id.main_rbtnMore);
        rbtnMy = findViewById(R.id.main_rbtnMy);
 
        rgroupTabMenu.setOnCheckedChangeListener(this);
    }
 
    //隐藏所有Fragment
    private void hideAllFragment(FragmentTransaction fragmentTransaction){
        if(fg1 != null)fragmentTransaction.hide(fg1);
        if(fg2 != null)fragmentTransaction.hide(fg2);
        if(fg3 != null)fragmentTransaction.hide(fg3);
        if(fg4 != null)fragmentTransaction.hide(fg4);
    }
 
    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        FragmentTransaction fTransaction = fManager.beginTransaction();
        hideAllFragment(fTransaction);
        switch (checkedId){
            case R.id.main_rbtnChannel:
                if(fg1 == null){
                    fg1 = new MyFragmentOne();
                    fTransaction.add(R.id.ly_content,fg1);
                }else{
                    fTransaction.show(fg1);
                }
                break;
            case R.id.main_rbtnMessage:
                if(fg2 == null){
                    fg2 = new MyFragmentTwo();
                    fTransaction.add(R.id.ly_content,fg2);
                }else{
                    fTransaction.show(fg2);
                }
                break;
            case R.id.main_rbtnMore:
                if(fg3 == null){
                    fg3 = new MyFragmentThree();
                    fTransaction.add(R.id.ly_content,fg3);
                }else{
                    fTransaction.show(fg3);
                }
                break;
            case R.id.main_rbtnMy:
                if(fg4 == null){
                    fg4 = new MyFragmentFour();
                    fTransaction.add(R.id.ly_content,fg4);
                }else{
                    fTransaction.show(fg4);
                }
                break;
        }
        fTransaction.commit();
    }
}           

複制

自定義Fragment類MyFragmentOne.java的代碼如下:

package com.deepreality.fragmentcaseonedemo;
 
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;
 
public class MyFragmentOne extends Fragment {
 
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fg_content_one, container, false);
        return view;
    }
}           

複制

以上就是Android 1對1直播源碼開發,底部導航欄的簡單實作的全部内容了。