转载请注明出处:http://blog.csdn.net/yangyu20121224/article/details/9122517
这篇文章是实现了百度贴吧一个老版本的悬浮式顶部和底部的标题栏效果,觉得还挺有意思的就想跟大家一起分
享一下,也算是对前面学过的UI设计知识的一个总结吧。好的,话不多说,请大家先看效果图。
一、实现效果图
二 、项目结构图
三 、详细代码编写
1、主布局Tab选项卡页面,maintabs_activity.xml:
- xml version="1.0" encoding="utf-8"?>
- <TabHost xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@android:id/tabhost"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent" >
-
- <LinearLayout
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:background="@color/backgroundcolor"
- android:orientation="vertical" >
-
- <FrameLayout
- android:id="@android:id/tabcontent"
- android:layout_width="fill_parent"
- android:layout_height="0.0dip"
- android:layout_weight="1.0" />
-
- <TabWidget
- android:id="@android:id/tabs"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_weight="0.0"
- android:visibility="gone" />
-
- <FrameLayout
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_gravity="bottom"
- android:layout_marginTop="-10.0dip"
- android:background="@drawable/maintab_toolbar_bg"
- android:paddingLeft="7.0dip"
- android:paddingRight="7.0dip" >
-
- <RadioGroup
- android:id="@+id/main_radio"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:gravity="center_vertical"
- android:orientation="horizontal" >
-
- <RadioButton
- android:id="@+id/radio_home"
- style="@style/main_tab_bottom"
- android:drawableTop="@drawable/tabs_home"
- android:text="@string/bar" />
-
- <RadioButton
- android:id="@+id/radio_mention"
- style="@style/main_tab_bottom"
- android:drawableTop="@drawable/tabs_sort"
- android:text="@string/remind" />
-
- <RadioButton
- android:id="@+id/radio_person_info"
- style="@style/main_tab_bottom"
- android:drawableTop="@drawable/tabs_search"
- android:text="@string/person_info" />
-
- <RadioButton
- android:id="@+id/radio_more"
- style="@style/main_tab_bottom"
- android:drawableTop="@drawable/tabs_more"
- android:text="@string/more" />
- RadioGroup>
-
- <TextView
- android:id="@+id/message_mention"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="top|left|center"
- android:layout_marginLeft="115.0dip"
- android:layout_marginTop="-5.0dip"
- android:background="@drawable/message_tips"
- android:gravity="center"
- android:textColor="#ffffffff"
- android:textSize="10sp"
- android:visibility="visible" />
-
- <TextView
- android:id="@+id/message_person"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="top|left|center"
- android:layout_marginLeft="190.0dip"
- android:layout_marginTop="-5.0dip"
- android:background="@drawable/message_tips"
- android:gravity="center"
- android:textColor="#ffffffff"
- android:textSize="13.0sp"
- android:visibility="visible" />
- FrameLayout>
- LinearLayout>
-
- TabHost>
2、贴吧首页布局页面,home_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="fill_parent"
- android:layout_height="fill_parent"
- android:background="@color/backgroundcolor"
- tools:ignore="ContentDescription" >
-
-
-
- <LinearLayout
- android:id="@+id/home_layout_bar"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_alignParentTop="true"
- android:layout_gravity="top"
- android:background="@drawable/title_bg"
- android:gravity="center" >
-
- <LinearLayout
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:background="@drawable/home_topbar_bg"
- android:gravity="center"
- android:orientation="horizontal" >
-
-
-
- <ImageButton
- android:id="@+id/home_bt_like"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:layout_gravity="center"
- android:layout_margin="0.0dip"
- android:layout_weight="1.0"
- android:background="@null"
- android:padding="0.0dip"
- android:paddingBottom="2.0dip"
- android:scaleType="center"
- android:src="@drawable/home_bt_like_on" />
-
-
-
- <ImageButton
- android:id="@+id/home_bt_mark"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:layout_gravity="center"
- android:layout_margin="0.0dip"
- android:layout_weight="1.0"
- android:background="@null"
- android:padding="0.0dip"
- android:paddingBottom="2.0dip"
- android:scaleType="center"
- android:src="@drawable/home_bt_mark" />
- LinearLayout>
- LinearLayout>
-
- <FrameLayout
- android:id="@+id/content"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:layout_below="@id/home_layout_bar"
- android:layout_marginTop="-12.0dip" />
-
- RelativeLayout>
3、首页贴吧布局页面中的其他两个Activity布局页面,“我的贴吧”Activity布局页面,home_my_activity.xml:
- xml version="1.0" encoding="utf-8"?>
- <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent" >
-
-
-
- <ImageView
- android:id="@+id/image_xianjian"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:src="@drawable/xianjian1"
- android:visibility="gone" />
-
- <ProgressBar
- android:id="@+id/home_progress_like"
- style="?android:attr/progressBarStyleInverse"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center"
- android:indeterminateDrawable="@drawable/progressbar"
- android:visibility="visible" />
-
- FrameLayout>
“我的标签”Activity布局页面,home_mark_activity.xml:
- xml version="1.0" encoding="utf-8"?>
- <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:layout_weight="1.0" >
-
-
-
- <ImageView
- android:id="@+id/imageView1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:src="@drawable/xianjian2" />
-
- FrameLayout>
4、“提醒”Activity布局页面,mention_activity.xml:
- xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/mention"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:background="#fff1f1f1" >
-
-
-
- <LinearLayout
- android:id="@+id/mention_layout_bar"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_alignParentTop="true"
- android:layout_margin="0.0dip"
- android:background="@drawable/title_bg" >
-
- <LinearLayout
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:layout_margin="0.0dip"
- android:background="@drawable/home_topbar_bg"
- android:orientation="horizontal" >
-
- <Button
- android:id="@+id/mention_bt_replyme"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:layout_gravity="center"
- android:layout_margin="0.0dip"
- android:layout_weight="1.0"
- android:background="@drawable/home_topbar_bt"
- android:clickable="false"
- android:gravity="center"
- android:padding="0.0dip"
- android:paddingBottom="2.0dip"
- android:text="@string/mention_replyme"
- android:textColor="#ffffffff"
- android:textSize="13.0sp" />
-
- <Button
- android:id="@+id/mention_bt_atme"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:layout_gravity="center"
- android:layout_margin="0.0dip"
- android:layout_weight="1.0"
- android:background="@null"
- android:clickable="true"
- android:gravity="center"
- android:padding="0.0dip"
- android:paddingBottom="2.0dip"
- android:text="@string/mention_atme"
- android:textColor="#ff90afff"
- android:textSize="13.0sp" />
- LinearLayout>
- LinearLayout>
-
- RelativeLayout>
5、“个人资料”布局页面,personalinfo_activity.xml:
6、“更多”布局页面,more_activity.xml:
- xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:background="@color/backgroundcolor" >
-
-
-
- <LinearLayout
- android:id="@+id/title"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_alignParentTop="true"
- android:layout_gravity="top"
- android:background="@drawable/title_bg" >
-
- <TextView
- android:id="@+id/titel_text"
- android:layout_width="0dp"
- android:layout_height="wrap_content"
- android:layout_gravity="center_vertical"
- android:layout_marginLeft="10.0dip"
- android:layout_marginRight="10.0dip"
- android:layout_weight="1.0"
- android:gravity="center_horizontal"
- android:singleLine="true"
- android:text="@string/more"
- android:textColor="#ffffffff"
- android:textSize="15.0sp" />
- LinearLayout>
-
- RelativeLayout>
7、布局页面就写完了,还有一些自定义按钮的资源文件我就不贴了,有需要的同学可以直接下载源码,下面是Java代码部分,主布局Tab选项卡Activity页面,MainActivity.java:
- package com.yangyu.mytitlebar02;
-
- import android.app.TabActivity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.widget.CompoundButton;
- import android.widget.CompoundButton.OnCheckedChangeListener;
- import android.widget.RadioButton;
- import android.widget.TabHost;
- import android.widget.TextView;
-
- import com.yangyu.mytitlebar02.home.HomeActivity;
-
-
-
-
-
- public class MainActivity extends TabActivity implements OnCheckedChangeListener {
-
- private static final String HOME_TAB = "home_tab";
- private static final String MENTION_TAB = "mention_tab";
- private static final String PERSON_TAB = "person_tab";
- private static final String MORE_TAB = "more_tab";
-
-
- private Intent mHomeIntent,mMentionIntent,mPersonIntent,mMoreIntent;
-
-
- private TabHost mTabHost;
-
-
- private RadioButton homeRb,mentionRb,personRb,moreRb;
-
-
- private TextView mMessageTipsMention,mMessageTipsPerson;
-
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.maintabs_activity);
-
- initView();
-
- initData();
- }
-
-
-
-
- private void initView(){
-
- mTabHost = getTabHost();
-
-
- mHomeIntent = new Intent(this, HomeActivity.class);
- mMentionIntent = new Intent(this, MentionActivity.class);
- mPersonIntent = new Intent(this, PersonInfoActivity.class);
- mMoreIntent = new Intent(this, MoreActivity.class);
-
-
- mMessageTipsMention = (TextView) findViewById(R.id.message_mention);
- mMessageTipsPerson = (TextView) findViewById(R.id.message_person);
-
-
- homeRb = ((RadioButton) findViewById(R.id.radio_home));
- mentionRb = ((RadioButton) findViewById(R.id.radio_mention));
- personRb = ((RadioButton) findViewById(R.id.radio_person_info));
- moreRb = ((RadioButton) findViewById(R.id.radio_more));
- }
-
-
-
-
- private void initData(){
-
- homeRb.setOnCheckedChangeListener(this);
- mentionRb.setOnCheckedChangeListener(this);
- personRb.setOnCheckedChangeListener(this);
- moreRb.setOnCheckedChangeListener(this);
-
-
- mMessageTipsMention.setText("2");
- mMessageTipsPerson.setText("4");
-
-
- mTabHost.addTab(buildTabSpec(HOME_TAB, mHomeIntent));
- mTabHost.addTab(buildTabSpec(MENTION_TAB, mMentionIntent));
- mTabHost.addTab(buildTabSpec(PERSON_TAB, mPersonIntent));
- mTabHost.addTab(buildTabSpec(MORE_TAB, mMoreIntent));
-
-
- homeRb.setChecked(true);
- mTabHost.setCurrentTabByTag(HOME_TAB);
- }
-
- private TabHost.TabSpec buildTabSpec(String tag, Intent intent) {
- TabHost.TabSpec tabSpec = mTabHost.newTabSpec(tag);
- tabSpec.setContent(intent).setIndicator("");
-
- return tabSpec;
- }
-
-
-
-
- public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
- if (isChecked) {
- switch (buttonView.getId()) {
- case R.id.radio_home:
- mTabHost.setCurrentTabByTag(HOME_TAB);
- break;
- case R.id.radio_mention:
- mTabHost.setCurrentTabByTag(MENTION_TAB);
-
- mMessageTipsMention.setVisibility(8);
- break;
- case R.id.radio_person_info:
- mTabHost.setCurrentTabByTag(PERSON_TAB);
- mMessageTipsPerson.setVisibility(8);
- break;
- case R.id.radio_more:
- mTabHost.setCurrentTabByTag(MORE_TAB);
- break;
- default:
- break;
- }
- }
- }
-
- }
8、贴吧首页Activity页面,HomeActivity.java:
- package com.yangyu.mytitlebar02.home;
-
- import android.app.ActivityGroup;
- import android.content.Intent;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.FrameLayout;
- import android.widget.ImageButton;
-
- import com.yangyu.mytitlebar02.R;
-
-
-
-
-
- public class HomeActivity extends ActivityGroup implements OnClickListener{
-
- private FrameLayout mContent;
-
-
- private ImageButton myButton,markButton;
-
-
- private static final String HOME_LIKE_ID = "like";
- private static final String HOME_MARK_ID = "mark";
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.home_activity);
-
- mContent = (FrameLayout) findViewById(R.id.content);
-
-
- myButton = (ImageButton) findViewById(R.id.home_bt_like);
- markButton = (ImageButton) findViewById(R.id.home_bt_mark);
-
-
- myButton.setOnClickListener(this);
- markButton.setOnClickListener(this);
-
-
- showMyView();
- }
-
-
-
-
- public void addView(String id, Class> clazz) {
- Intent intent = new Intent(this, clazz);
-
- mContent.removeAllViews();
- mContent.addView(getLocalActivityManager().startActivity(id, intent).getDecorView());
- }
-
- @Override
- public void onClick(View v) {
- switch (v.getId()) {
- case R.id.home_bt_like:
- showMyView();
- break;
- case R.id.home_bt_mark:
- showMarkView();
- break;
- default:
- break;
- }
- }
-
-
-
-
- private void showMyView(){
- addView(HOME_LIKE_ID, MyActivity.class);
- myButton.setBackgroundResource(R.drawable.home_topbar_bt);
- myButton.setImageResource(R.drawable.home_bt_like_on);
- markButton.setBackgroundDrawable(null);
- markButton.setImageResource(R.drawable.home_bt_mark);
- }
-
-
-
-
- private void showMarkView(){
- addView(HOME_MARK_ID, MarkActivity.class);
- markButton.setBackgroundResource(R.drawable.home_topbar_bt);
- markButton.setImageResource(R.drawable.home_bt_mark_on);
- myButton.setBackgroundDrawable(null);
- myButton.setImageResource(R.drawable.home_bt_like);
- }
- }
9、其他的Activity代码我就不贴了,都只是加载了一个布局,例如“提醒”Activity页面,MentionActivity.java:
- package com.yangyu.mytitlebar02;
-
- import android.app.Activity;
- import android.os.Bundle;
-
-
-
-
-
- public class MentionActivity extends Activity {
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.mention_activity);
- }
-
- }
源码下载地址