Android 1对1直播源码开发,底部导航栏的简单实现

2021-06-11 17:57:12 浏览数 (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

代码语言:javascript复制
<?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

代码语言:javascript复制
<?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布局代码如下:

代码语言:javascript复制
<?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的代码如下:

代码语言:javascript复制
<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的代码如下:

代码语言:javascript复制
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的代码如下:

代码语言:javascript复制
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直播源码开发,底部导航栏的简单实现的全部内容了。

0 人点赞