前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Android 1对1直播源码开发,底部导航栏的简单实现

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

作者头像
云豹科技程序员
修改2021-06-11 17:57:12
5760
修改2021-06-11 17:57:12
举报

在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直播源码开发,底部导航栏的简单实现的全部内容了。

本文系转载,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文系转载前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
云直播
云直播(Cloud Streaming Services,CSS)为您提供极速、稳定、专业的云端直播处理服务,根据业务的不同直播场景需求,云直播提供了标准直播、快直播、云导播台三种服务,分别针对大规模实时观看、超低延时直播、便捷云端导播的场景,配合腾讯云视立方·直播 SDK,为您提供一站式的音视频直播解决方案。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档