<?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:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.mesmerize.simpleapp.MainActivity">
<RadioGroup
android:id="@+id/main_radio"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_alignParentBottom="true"
android:background="@mipmap/bottom_background"
android:gravity="center_vertical"
android:orientation="horizontal"
android:paddingTop="3dp">
<RadioButton
android:id="@+id/rb_recommend"
style="@style/main_tab_bottom"
android:drawableTop="@drawable/icon_recommend"
android:text="推荐" />
<RadioButton
android:id="@+id/rb_go"
style="@style/main_tab_bottom"
android:drawableTop="@drawable/icon_go"
android:text="逛" />
<RadioButton
android:id="@+id/rb_design"
style="@style/main_tab_bottom"
android:drawableTop="@drawable/icon_design"
android:text="设计师" />
<RadioButton
android:id="@+id/rb_my"
style="@style/main_tab_bottom"
android:drawableTop="@drawable/icon_personal"
android:text="我的" />
</RadioGroup>
<FrameLayout
android:id="@+id/flc_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/main_radio"></FrameLayout>
</RelativeLayout>
<!--首页底部按钮-->
<style name="main_tab_bottom">
<item name="android:textSize">11.0sp</item>
<item name="android:textColor">@color/bottom_text_color</item>
<item name="android:ellipsize">marquee</item>
<item name="android:gravity">center</item>
<item name="android:background">#00000000</item>
<item name="android:paddingTop">1dp</item>
<item name="android:layout_width">0dp</item>
<item name="android:layout_height">match_parent</item>
<item name="android:button">@null</item>
<item name="android:singleLine">true</item>
<item name="android:layout_weight">1.0</item>
</style>
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="#ff1753"/>
<item android:state_checked="true" android:color="#ff1753"/>
<item android:color="#646464"/> <!-- not selected -->
</selector>
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@mipmap/index_pressed" android:state_pressed="true"></item>
<item android:drawable="@mipmap/index_pressed" android:state_checked="true"></item>
<item android:drawable="@mipmap/index"/>
</selector>
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@mipmap/go_pressed" android:state_pressed="true"></item>
<item android:drawable="@mipmap/go_pressed" android:state_checked="true"></item>
<item android:drawable="@mipmap/go"/>
</selector>
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@mipmap/design_pressed" android:state_pressed="true"></item>
<item android:drawable="@mipmap/design_pressed" android:state_checked="true"></item>
<item android:drawable="@mipmap/design"/>
</selector>
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@mipmap/my_pressed" android:state_pressed="true"></item>
<item android:drawable="@mipmap/my_pressed" android:state_checked="true"></item>
<item android:drawable="@mipmap/my"/>
</selector>
package com.mesmerize.simpleapp;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.RadioButton;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private RadioButton rb_recommend;
private RadioButton rb_go;
private RadioButton rb_design;
private RadioButton rb_my;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initListener();
}
/**
* 初始化 View 控件
*/
private void initView(){
rb_recommend = (RadioButton) findViewById(R.id.rb_recommend);
rb_go = (RadioButton) findViewById(R.id.rb_go);
rb_design = (RadioButton) findViewById(R.id.rb_design);
rb_my = (RadioButton) findViewById(R.id.rb_my);
}
/**
* 初始化 事件监听事件
*/
private void initListener(){
rb_recommend.setOnClickListener(this);
rb_go.setOnClickListener(this);
rb_design.setOnClickListener(this);
rb_my.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.rb_recommend:
Toast.makeText(this, "选中了推荐页面", Toast.LENGTH_SHORT).show();
break;
case R.id.rb_go:
Toast.makeText(this, "选中了逛页面", Toast.LENGTH_SHORT).show();
break;
case R.id.rb_design:
Toast.makeText(this, "选中了设计师页面", Toast.LENGTH_SHORT).show();
break;
case R.id.rb_my:
Toast.makeText(this, "选中了我的页面", Toast.LENGTH_SHORT).show();
break;
}
}
}
package com.mesmerize.simpleapp;
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;
/**
* Created by mesmerize on 2016/12/28.
*/
public class RecommendFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_recommend,null);
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:text="我是推荐Fragment"
android:textSize="20sp"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
package com.mesmerize.simpleapp;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.RadioButton;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private RadioButton rb_recommend;
private RadioButton rb_go;
private RadioButton rb_design;
private RadioButton rb_my;
private RecommendFragment mRecommendFragment;
private GoFragment mGoFragment;
private DesignFragment mDesignFragment;
private PersonalFragment mPersonalFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initListener();
initDefaultFragment();
}
/**
* 初始化 View 控件
*/
private void initView(){
rb_recommend = (RadioButton) findViewById(R.id.rb_recommend);
rb_go = (RadioButton) findViewById(R.id.rb_go);
rb_design = (RadioButton) findViewById(R.id.rb_design);
rb_my = (RadioButton) findViewById(R.id.rb_my);
}
/**
* 初始化 事件监听事件
*/
private void initListener(){
rb_recommend.setOnClickListener(this);
rb_go.setOnClickListener(this);
rb_design.setOnClickListener(this);
rb_my.setOnClickListener(this);
}
/**
* 初始化 默认显示的 Fragment
*/
private void initDefaultFragment(){
FragmentManager fm = getFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
transaction.replace(R.id.fl_content, new RecommendFragment());
transaction.commit();
rb_recommend.setChecked(true);
}
@Override
public void onClick(View v) {
FragmentManager fm = getFragmentManager();
// 开启事务
FragmentTransaction transaction = fm.beginTransaction();
switch (v.getId()) {
case R.id.rb_recommend:
if (mRecommendFragment == null) {
mRecommendFragment = new RecommendFragment();
}
// 替换
transaction.replace(R.id.fl_content, mRecommendFragment);
break;
case R.id.rb_go:
if (mGoFragment == null) {
mGoFragment = new GoFragment();
}
// 替换
transaction.replace(R.id.fl_content, mGoFragment);
break;
case R.id.rb_design:
if (mDesignFragment == null) {
mDesignFragment = new DesignFragment();
}
// 替换
transaction.replace(R.id.fl_content, mDesignFragment);
break;
case R.id.rb_my:
if (mPersonalFragment == null) {
mPersonalFragment = new PersonalFragment();
}
// 替换
transaction.replace(R.id.fl_content, mPersonalFragment);
break;
}
// 提交事务
transaction.commit();
}
}
431.19 KB, 阅读权限: 10, 下载次数: 17
登录即可下载
相反人生 发表于 2016-12-28 18:15
新人给跪了 我以后一定也可以的
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) | 黑马程序员IT技术论坛 X3.2 |