安卓项目之淘忆2.0的代码实现之主页底部的导航栏功能

来源:互联网 发布:finale for mac 编辑:程序博客网 时间:2024/05/22 10:35

从之前的设计图可以见的,我用的是底部导航的设计,结合RadioButton和RadioGroup的功能,将图片和文字进行特效处理,所以做出现在这样,点击后有效果的。

下面看一下:

main_activity.xml的文件内容:

<?xml version="1.0" encoding="utf-8"?><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="#f8f8f8"    tools:context=".ui.activity.MainActivity">    <FrameLayout        android:id="@+id/fl_container"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:layout_above="@+id/ll_nav">    </FrameLayout>    <LinearLayout        android:id="@+id/ll_nav"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_alignParentBottom="true"        android:layout_gravity="bottom"        android:background="#fff"        android:orientation="horizontal"        android:padding="5dp">        <RadioGroup            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:orientation="horizontal">            <RadioButton                android:id="@+id/rb_home"                android:layout_width="0dp"                android:layout_height="wrap_content"                android:layout_gravity="bottom"                android:layout_weight="1"                android:background="@android:color/transparent"                android:button="@null"                android:checked="true"                android:drawableTop="@drawable/nav_rb_home"                android:gravity="center_horizontal|bottom"                android:paddingTop="2dp"                android:text="首页"                android:textColor="@drawable/nav_rb_text"                android:textSize="12sp" />            <RadioButton                android:id="@+id/rb_theme"                android:layout_width="0dp"                android:layout_height="wrap_content"                android:layout_gravity="bottom"                android:layout_weight="1"                android:background="@android:color/transparent"                android:button="@null"                android:drawableTop="@drawable/nav_rb_theme"                android:gravity="center_horizontal|bottom"                android:paddingTop="2dp"                android:text="专题"                android:textColor="@drawable/nav_rb_text"                android:textSize="12sp" />            <RadioButton                android:id="@+id/rb_share"                android:layout_width="0dp"                android:layout_height="wrap_content"                android:layout_gravity="bottom"                android:layout_weight="1"                android:background="@android:color/transparent"                android:button="@null"                android:drawableTop="@drawable/nav_rb_share"                android:gravity="center_horizontal|bottom"                android:paddingTop="2dp"                android:text="分享"                android:textColor="@drawable/nav_rb_text"                android:textSize="12sp" />            <RadioButton                android:id="@+id/rb_find"                android:layout_width="0dp"                android:layout_height="wrap_content"                android:layout_gravity="bottom"                android:layout_weight="1"                android:background="@android:color/transparent"                android:button="@null"                android:drawableTop="@drawable/nav_rb_find"                android:gravity="center_horizontal|bottom"                android:paddingTop="2dp"                android:text="发现"                android:textColor="@drawable/nav_rb_text"                android:textSize="12dp" />            <RadioButton                android:id="@+id/rb_user"                android:layout_width="0dp"                android:layout_height="wrap_content"                android:layout_gravity="bottom"                android:layout_weight="1"                android:background="@android:color/transparent"                android:button="@null"                android:drawableTop="@drawable/nav_rb_user"                android:gravity="center_horizontal|bottom"                android:paddingTop="2dp"                android:text="我的"                android:textColor="@drawable/nav_rb_text"                android:textSize="12sp" />        </RadioGroup>    </LinearLayout></RelativeLayout>
下面是MainActivity.class的文件内容:

package com.elainetaylor.taoyi.ui.activity;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.widget.RadioButton;import com.avos.avoscloud.AVOSCloud;import com.elainetaylor.taoyi.R;import com.elainetaylor.taoyi.ui.fragment.FindFragment;import com.elainetaylor.taoyi.ui.fragment.HomeFragment;import com.elainetaylor.taoyi.ui.fragment.ShareFragment;import com.elainetaylor.taoyi.ui.fragment.ThemeFragment;import com.elainetaylor.taoyi.ui.fragment.UserFragment;public class MainActivity extends AppCompatActivity implements View.OnClickListener {    private RadioButton rbHome, rbTheme, rbShare, rbFind, rbUser;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        AVOSCloud.initialize(this, "K1Ki1nEU9GFmUK6lfVMBMeHE-9Nh9j0Va", "pwYculXduYevPHtGDk6LuI0c");        init();    }    public void init() {        rbHome = (RadioButton) findViewById(R.id.rb_home);        rbTheme = (RadioButton) findViewById(R.id.rb_theme);        rbShare = (RadioButton) findViewById(R.id.rb_share);        rbFind = (RadioButton) findViewById(R.id.rb_find);        rbUser = (RadioButton) findViewById(R.id.rb_user);        rbHome.setOnClickListener(this);        rbTheme.setOnClickListener(this);        rbShare.setOnClickListener(this);        rbFind.setOnClickListener(this);        rbUser.setOnClickListener(this);        getSupportFragmentManager().beginTransaction().replace(R.id.fl_container, new HomeFragment()).commit();    }    @Override    public void onClick(View view) {        switch (view.getId()) {            case R.id.rb_home:                getSupportFragmentManager().beginTransaction().replace(R.id.fl_container, new HomeFragment()).commit();                break;            case R.id.rb_theme:                getSupportFragmentManager().beginTransaction().replace(R.id.fl_container, new ThemeFragment()).commit();                break;            case R.id.rb_share:                getSupportFragmentManager().beginTransaction().replace(R.id.fl_container, new ShareFragment()).commit();                break;            case R.id.rb_find:                getSupportFragmentManager().beginTransaction().replace(R.id.fl_container, new FindFragment()).commit();                break;            case R.id.rb_user:                getSupportFragmentManager().beginTransaction().replace(R.id.fl_container, new UserFragment()).commit();                break;        }    }}
下面介绍一样例nav_rb_home.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/nav_home_pre" />    <item android:state_checked="false"        android:drawable="@mipmap/nav_home"/></selector>
很简单吧,偶是个小白,所以只会写写这种小东西,今天先到这里啦。。。

0 0
原创粉丝点击