Android基础入门教程 2015年最新Android基础入门教程目录 背景相关与系统架构分析 开发环境搭建 使用Eclipse + ADT + SDK开发Android APP 使用Android Studio开发Android APP SDK更新不了问题解决 Genymotion模拟器安装 Git使用教程之本地仓库的基本操作 Git之使用GitHub搭建远程仓库 (九妹)图片怎么玩 界面原型设计 工程相关解析(各种文件,资源访问) Android程序签名打包 反编译APK获取代码&资源 反编译APK获取代码&资源 View与ViewGroup的概念 LinearLayout(线性布局) RelativeLayout(相对布局) TableLayout(表格布局) FrameLayout(帧布局) GridLayout(网格布局) AbsoluteLayout(绝对布局) TextView(文本框)详解 EditText(输入框)详解 Button(按钮)与ImageButton(图像按钮) ImageView(图像视图) RadioButton(单选按钮)&Checkbox(复选框) 开关按钮ToggleButton和开关Switch ProgressBar(进度条) SeekBar(拖动条) RatingBar(星级评分条) ScrollView(滚动条) Date & Time组件(上) Date & Time组件(下) Adapter基础讲解 ListView简单实用 BaseAdapter优化 ListView的焦点问题 ListView之checkbox错位问题解决 ListView的数据更新问题 构建一个可复用的自定义BaseAdapter ListView Item多布局的实现 GridView(网格视图)的基本使用 Spinner(列表选项框)的基本使用 AutoCompleteTextView(自动完成文本框)的基本使用 ExpandableListView(可折叠列表)的基本使用 ViewFlipper(翻转视图)的基本使用 Toast(吐司)的基本使用 Notification(状态栏通知)详解 AlertDialog(对话框)详解 其他几种常用对话框基本使用 PopupWindow(悬浮框)的基本使用 菜单(Menu) ViewPager的简单使用 DrawerLayout(官方侧滑菜单)的简单使用 基于监听的事件处理机制 基于回调的事件处理机制 Handler消息传递机制浅析 TouchListener PK OnTouchEvent + 多点触碰 监听EditText的内容变化 响应系统设置的事件(Configuration类) AnsyncTask异步任务 Gestures(手势) Activity初学乍练 Activity初窥门径 Activity登堂入室 Service初涉 Service进阶 Service精通 BroadcastReceiver牛刀小试 BroadcastReceiver庖丁解牛 ContentProvider初探 ContentProvider再探——Document Provider Intent的基本使用 Intent之复杂数据的传递 Fragment基本概述 Fragment实例精讲——底部导航栏的实现(方法1) Fragment实例精讲——底部导航栏的实现(方法2) Fragment实例精讲——底部导航栏的实现(方法3) Fragment实例精讲——底部导航栏+ViewPager滑动切换页面 Fragment实例精讲——新闻(购物)类App列表Fragment的简单实现 数据存储与访问之——文件存储读写 数据存储与访问之——SharedPreferences保存用户偏好参数 数据存储与访问之——初见SQLite数据库 数据存储与访问之——又见SQLite数据库 Android网络编程要学的东西与Http协议学习 Android Http请求头与响应头的学习 Android HTTP请求方式:HttpURLConnection Android HTTP请求方式:HttpClient Android XML数据解析 Android JSON数据解析 Android 文件上传 Android 文件下载(1) Android 文件下载(2) Android 调用 WebService WebView(网页视图)基本用法 WebView和JavaScrip交互基础 Android 4.4后WebView的一些注意事项 WebView文件下载 WebView缓存问题 WebView处理网页返回的错误码信息 Socket学习网络基础准备 基于TCP协议的Socket通信(1) 分类 Android 基础入门教程 基于TCP协议的Socket通信(2) 基于UDP协议的Socket通信 Android中的13种Drawable小结 Part 1 Android中的13种Drawable小结 Part 2 Android中的13种Drawable小结 Part 3 Bitmap(位图)全解析 Part 1 Bitmap引起的OOM问题 三个绘图工具类详解 绘图类实战示例 Paint API之—— MaskFilter(面具) Paint API之—— Xfermode与PorterDuff详解(一) Paint API之—— Xfermode与PorterDuff详解(二) Paint API之—— Xfermode与PorterDuff详解(三) Paint API之—— Xfermode与PorterDuff详解(四) Paint API之—— Xfermode与PorterDuff详解(五) Paint API之—— ColorFilter(颜色过滤器)(1/3) Paint API之—— ColorFilter(颜色过滤器)(2-3) Paint API之—— ColorFilter(颜色过滤器)(3-3) Paint API之—— PathEffect(路径效果) Paint API之—— Shader(图像渲染) Paint几个枚举/常量值以及ShadowLayer阴影效果 Paint API之——Typeface(字型) Canvas API详解(Part 1) Canvas API详解(Part 2)剪切方法合集 Canvas API详解(Part 3)Matrix和drawBitmapMash Android动画合集之帧动画 Android动画合集之补间动画 Android动画合集之属性动画-初见 Android动画合集之属性动画-又见 使用SoundPool播放音效(Duang~) MediaPlayer播放音频与视频 使用Camera拍照 使用MediaRecord录音 TelephonyManager(电话管理器) SmsManager(短信管理器) AudioManager(音频管理器) Vibrator(振动器) AlarmManager(闹钟服务) PowerManager(电源服务) WindowManager(窗口管理服务) LayoutInflater(布局服务) WallpaperManager(壁纸管理器) 传感器专题(1)——相关介绍 传感器专题(2)——方向传感器 传感器专题(3)——加速度/陀螺仪传感器 传感器专题(4)——其他传感器了解 Android GPS初涉 《2015最新Android基础入门教程》完结散花~

Android 开发手册

浏览86891
更新时间2022-04-15

本节引言:

上一节中我们使用LinearLayout + TextView实现了底部导航栏的效果,每次点击我们都要重置 所有TextView的状态,然后选中点击的TextView,有点麻烦是吧,接下来我们用另一种方法: RadioGroup + RadioButton来实现我们上一节的效果!


1.一些碎碎念

本节用到的是实现单选效果的RadioButton,如果你不熟悉,或者没用过,可先移步到:RadioButton简单点说就是我们就是一个RadioGroup包着四个RadioButton,和前面一样用比例来划分:1:1:1:1;
另外我们只需重写RadioGroup的onCheckedChange,判断checkid即可知道点击的是哪个RadioButton! 好的,下面开始堆码!


2.实现流程

PS:这里的素材什么的,直接使用的是上一节中的素材!另外drawable类的资源都是将selected 状态修改成checked!


Step 1:写底部选项的一些资源文件

图片Drawable资源:tab_menu_channel.xml

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

其他三个照葫芦画瓢!

文字资源:tab_menu_text.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@color/text_yellow" android:state_checked="true" />
    <item android:color="@color/text_gray" />
</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_selected="true">
        <shape>
            <solid android:color="#FFC4C4C4" />
        </shape>
    </item>
    <item>
        <shape>
            <solid android:color="@color/transparent" />
        </shape>
    </item>
</selector>

Step 2:编写我们的Activity布局

在前面用TextView实现底部导航栏我们就发现了一个问题,每个TextView的属性都几乎是差不多 的,而在建议那里我们也说让大家把相同的属性抽取出来写到Style中,可能部分朋友懒或者不知道 如何抽取出来,以及用,这里就给大家示范下:

首先我们取出其中一个RadioGroup的标签:

<RadioButton
            android:id="@+id/rb_channel"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="../style/images/tab_menu_bg"
            android:button="@null"
            android:drawableTop="@drawable/tab_menu_channel"
            android:gravity="center"
            android:paddingTop="3dp"
            android:text="@string/tab_menu_alert"
            android:textColor="@drawable/tab_menu_text"
            android:textSize="18sp" />

我们可以把每个RadioButton都相同的属性抽取出来,写到style.xml文件中:

<style name="tab_menu_item">
    <item name="android:layout_width">0dp</item>
    <item name="android:layout_weight">1</item>
    <item name="android:layout_height">match_parent</item>
    <item name="android:background">@drawable/tab_menu_bg</item>
    <item name="android:button">@null</item>
    <item name="android:gravity">center</item>
    <item name="android:paddingTop">3dp</item>
    <item name="android:textColor">@drawable/tab_menu_text</item>
    <item name="android:textSize">18sp</item>
</style>

然后我们的activity_main.xml中的RadioButton就用不着次次都写相同的代码了, 只需让RadioButton的style="@style/tab_menu_item"就可以了!

activity_main.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"
    android:background="../style/images/bg_gray"
    tools:context=".MainActivity">


    <RelativeLayout
        android:id="@+id/ly_top_bar"
        android:layout_width="match_parent"
        android:layout_height="48dp"
        android:background="../style/images/bg_topbar">

        <TextView
            android:id="@+id/txt_topbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_centerInParent="true"
            android:gravity="center"
            android:text="信息"
            android:textColor="@color/text_topbar"
            android:textSize="18sp" />

        <View
            android:layout_width="match_parent"
            android:layout_height="2px"
            android:layout_alignParentBottom="true"
            android:background="../style/images/div_white" />

    </RelativeLayout>

    <RadioGroup
        android:id="@+id/rg_tab_bar"
        android:layout_width="match_parent"
        android:layout_height="56dp"
        android:layout_alignParentBottom="true"
        android:background="../style/images/bg_white"
        android:orientation="horizontal">

        <RadioButton
            android:id="@+id/rb_channel"
            style="@style/tab_menu_item"
            android:drawableTop="@drawable/tab_menu_channel"
            android:text="@string/tab_menu_alert" />

        <RadioButton
            android:id="@+id/rb_message"
            style="@style/tab_menu_item"
            android:drawableTop="@drawable/tab_menu_message"
            android:text="@string/tab_menu_profile" />

        <RadioButton
            android:id="@+id/rb_better"
            style="@style/tab_menu_item"
            android:drawableTop="@drawable/tab_menu_better"
            android:text="@string/tab_menu_pay" />

        <RadioButton
            android:id="@+id/rb_setting"
            style="@style/tab_menu_item"
            android:drawableTop="@drawable/tab_menu_setting"
            android:text="@string/tab_menu_setting"/>

    </RadioGroup>

    <View
        android:id="@+id/div_tab_bar"
        android:layout_width="match_parent"
        android:layout_height="2px"
        android:layout_above="@id/rg_tab_bar"
        android:background="../style/images/div_white" />

    <FrameLayout
        android:id="@+id/ly_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@id/div_tab_bar"
        android:layout_below="@id/ly_top_bar"></FrameLayout>

</RelativeLayout>

Step 3:隐藏顶部导航栏

AndroidManifest.xml设置下theme属性

android:theme="@style/Theme.AppCompat.NoActionBar"

Step 4:创建一个Fragment的简单布局与类:

直接照搬上一节的布局跟Fragment:

fg_content.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="../style/images/bg_white">

    <TextView
        android:id="@+id/txt_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="呵呵"
        android:textColor="@color/text_yellow"
        android:textSize="20sp"/>

</LinearLayout>

MyFragment.java:

/**
 * Created by Coder-pig on 2015/8/29 0028.
 */
public class MyFragment extends Fragment {

    private String content;
    public MyFragment(String content) {
        this.content = content;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fg_content,container,false);
        TextView txt_content = (TextView) view.findViewById(R.id.txt_content);
        txt_content.setText(content);
        return view;
    }
}

Step 5:编写MainActivity.java

这个比起TextView实现简单多了,就不详细讲解了,很简单,直接上代码:

MainActivity.java

/**
 * Created by Coder-pig on 2015/8/29 0028.
 */
public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener{

    private RadioGroup rg_tab_bar;
    private RadioButton rb_channel;

    //Fragment Object
    private MyFragment fg1,fg2,fg3,fg4;
    private FragmentManager fManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        fManager = getFragmentManager();
        rg_tab_bar = (RadioGroup) findViewById(R.id.rg_tab_bar);
        rg_tab_bar.setOnCheckedChangeListener(this);
        //获取第一个单选按钮,并设置其为选中状态
        rb_channel = (RadioButton) findViewById(R.id.rb_channel);
        rb_channel.setChecked(true);
    }


    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        FragmentTransaction fTransaction = fManager.beginTransaction();
        hideAllFragment(fTransaction);
        switch (checkedId){
            case R.id.rb_channel:
                if(fg1 == null){
                    fg1 = new MyFragment("第一个Fragment");
                    fTransaction.add(R.id.ly_content,fg1);
                }else{
                    fTransaction.show(fg1);
                }
                break;
            case R.id.rb_message:
                if(fg2 == null){
                    fg2 = new MyFragment("第二个Fragment");
                    fTransaction.add(R.id.ly_content,fg2);
                }else{
                    fTransaction.show(fg2);
                }
                break;
            case R.id.rb_better:
                if(fg3 == null){
                    fg3 = new MyFragment("第三个Fragment");
                    fTransaction.add(R.id.ly_content,fg3);
                }else{
                    fTransaction.show(fg3);
                }
                break;
            case R.id.rb_setting:
                if(fg4 == null){
                    fg4 = new MyFragment("第四个Fragment");
                    fTransaction.add(R.id.ly_content,fg4);
                }else{
                    fTransaction.show(fg4);
                }
                break;
        }
        fTransaction.commit();
    }

    //隐藏所有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);
    }

}

PS:在上一节忘记讲一点了,FragmentTransaction只能使用一次,每次使用都要调用FragmentManager 的beginTransaction()方法获得FragmentTransaction事务对象哦!


3.运行效果图

其实和上一节实现的效果是一样的:

1.gif


4.代码下载:

FragmentDemo2.zip:FragmentDemo2.zip 下载


5.本节小结:

本节讲解的是实现底部导航栏的第二种方法:RadioGroup + RadioButton,有了单选,我们 就不用像TextView一样,每次点击后先重置所有TextView的Selected状态,再让点击的TextView 的Selected为true,这样就可以写少一点代码了~本节就到这里~谢谢

相关视频

更多

免费

微信小程序--企业微网站
初级微信小程序--企业微网站

5.5万次学习

收藏

免费

微信小程序开发基础教程
初级微信小程序开发基础教程

6.9万次学习

收藏

免费

微信小程序基础API篇
高级微信小程序基础API篇

3.6万次学习

收藏

免费

微信小程序--微商城
高级微信小程序--微商城

4万次学习

收藏

精品课程

更多
前端入门_HTML5
前端入门_HTML5

共29课时

65.2万人学习

CSS视频教程-玉女心经版
CSS视频教程-玉女心经版

共25课时

41.4万人学习

JavaScript极速入门_玉女心经系列
JavaScript极速入门_玉女心经系列

共43课时

77万人学习

独孤九贱(1)_HTML5视频教程
独孤九贱(1)_HTML5视频教程

共25课时

64.3万人学习

独孤九贱(2)_CSS视频教程
独孤九贱(2)_CSS视频教程

共22课时

24.5万人学习

独孤九贱(3)_JavaScript视频教程
独孤九贱(3)_JavaScript视频教程

共28课时

36.4万人学习

独孤九贱(4)_PHP视频教程
独孤九贱(4)_PHP视频教程

共89课时

134.1万人学习

热门下载

更多
phpStudy 2018最新版
phpStudy 2018最新版

集成PHP7,16种组合,超全大合集

下载

VC9 32位
VC9 32位

VC9 32位 phpstudy集成安装环境运行库

下载

VC11 32位
VC11 32位

VC11 32位 phpstudy集成安装环境运行库​

下载

php程序员工具箱完整版
php程序员工具箱完整版

程序员工具箱 v1.0 php集成环境

下载

VC14 32位
VC14 32位

VC14 32位 phpstudy安装环境运行库

下载

SublimeText3汉化版
SublimeText3汉化版

中文版,非常好用

下载

Notepad++ Windows版
Notepad++ Windows版

Notepad++ 8.9.7 Windows版官方安装包,适合 Windows 用户进行代码编辑、文本处理、语法高亮和插件扩展配置。

下载