安卓顶部标题栏部分有一半看不见,好像和标题栏重叠了

Android项目中自定义顶部标题栏
Android项目中自定义顶部标题栏
下面给大家详细介绍android中自定义顶部标题栏的思路及实现方式
思路及实现步骤
1.定义标题栏布局
2.自定义TitleActivity控制标题栏按钮监听
3.在TitleActivity中实现标题栏以下内容切换
首先定义标题栏
定义控制标题栏按钮和标题栏以下内容的布局
&framelayout android:background="#fff" android:id="@+id/layout_content" android:layout_height="match_parent" android:layout_width="match_parent"&
&/framelayout&
注:此处使用
标签引入标题栏,且下方有定义一个空的FrameLayout的布局。
定义TitleActivity控制按钮及布局
package org.gaochun.
import org.gaochun.ui.R;
import android.app.A
import android.os.B
import android.view.V
import android.view.View.OnClickL
import android.view.ViewGroup.LayoutP
import android.widget.B
import android.widget.FrameL
import android.widget.TextV
import android.widget.T
* @author gao_chun
* 自定义标题栏
public class TitleActivity extends Activity implements OnClickListener{
//private RelativeLayout mLayoutTitleB
private TextView mTitleTextV
private Button mBackwardbB
private Button mForwardB
private FrameLayout mContentL
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setupViews();
//加载 activity_title 布局 ,并获取标题及两侧按钮
private void setupViews() {
super.setContentView(R.layout.activity_title);
mTitleTextView = (TextView) findViewById(R.id.text_title);
mContentLayout = (FrameLayout) findViewById(R.id.layout_content);
mBackwardbButton = (Button) findViewById(R.id.button_backward);
mForwardButton = (Button) findViewById(R.id.button_forward);
* 是否显示返回按钮
* @param backwardResid
* @param show
true则显示
protected void showBackwardView(int backwardResid, boolean show) {
if (mBackwardbButton != null) {
if (show) {
mBackwardbButton.setText(backwardResid);
mBackwardbButton.setVisibility(View.VISIBLE);
mBackwardbButton.setVisibility(View.INVISIBLE);
} // else ignored
* 提供是否显示提交按钮
* @param forwardResId
* @param show
true则显示
protected void showForwardView(int forwardResId, boolean show) {
if (mForwardButton != null) {
if (show) {
mForwardButton.setVisibility(View.VISIBLE);
mForwardButton.setText(forwardResId);
mForwardButton.setVisibility(View.INVISIBLE);
} // else ignored
* 返回按钮点击后触发
* @param backwardView
protected void onBackward(View backwardView) {
Toast.makeText(this, 点击返回,可在此处调用finish(), Toast.LENGTH_LONG).show();
//finish();
* 提交按钮点击后触发
* @param forwardView
protected void onForward(View forwardView) {
Toast.makeText(this, 点击提交, Toast.LENGTH_LONG).show();
//设置标题内容
public void setTitle(int titleId) {
mTitleTextView.setText(titleId);
//设置标题内容
public void setTitle(CharSequence title) {
mTitleTextView.setText(title);
//设置标题文字颜色
public void setTitleColor(int textColor) {
mTitleTextView.setTextColor(textColor);
//取出FrameLayout并调用父类removeAllViews()方法
public void setContentView(int layoutResID) {
mContentLayout.removeAllViews();
View.inflate(this, layoutResID, mContentLayout);
onContentChanged();
public void setContentView(View view) {
mContentLayout.removeAllViews();
mContentLayout.addView(view);
onContentChanged();
/* (non-doc)
* @see android.app.Activity#setContentView(android.view.View, android.view.ViewGroup.LayoutParams)
public void setContentView(View view, LayoutParams params) {
mContentLayout.removeAllViews();
mContentLayout.addView(view, params);
onContentChanged();
/* (non-Javadoc)
* @see android.view.View.OnClickListener#onClick(android.view.View)
* 按钮点击调用的方法
public void onClick(View v) {
switch (v.getId()) {
case R.id.button_backward:
onBackward(v);
case R.id.button_forward:
onForward(v);
MainActivity中调用时直接 extends TitleActivity 使用之前在TitleActivity中定义的方法Android去掉标题栏点菜单键程序崩溃
在安卓以前的版本中,我们会使用以下两种方式来去掉顶部自带的标题栏:
1、在代码中实现:
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
2、在Manifest.xml中实现:
&application android:icon=&@drawable/icon& &&
& & & & android:label=&@string/app_name& &&
& & & & android:theme=&@android:style/Theme.NoTitleBar&&&
或者先在style文件中定义
&?xml version=&1.0& encoding=&UTF-8& ?& &
&resources& &
& & &style name=&notitle&& &&item name=&android:windowNoTitle&&true&/item& &&/style& &&
&/resources&
之后在中引用
&application android:icon=&@drawable/icon& &&
& & & & android:label=&@string/app_name& &&
& & & & android:theme=&@style/notitle&&
但是新版android推荐使用的是继承ActionBarActivity的Activity,这时再使用上述两种方法去掉标题栏,当应用点击菜单键的时候则会出现空指针异常的错误,或者APP会出现闪退。
这是因为Activity继承了ActionBarActivity,它来自android.support.v7.app.ActionBarActivity。
所以就要使用与其配合的AppCompat的theme才行
解决的办法之一就是让Activity去继承Activity而不是ActionBarActivity。
如果非要继承ActionBarActivity的话,这时可以用下面的方法来去掉标题栏:
在Activity的onCreate方法中,在setContentView(R.layout.activity_main);之前添加
ActionBar actionBar = getSupportActionBar();
actionBar.hide();
这两行代码即可去掉标题栏。【Android UI设计与开发】第15期:顶部标题栏(六)实现悬浮式顶部和底部标题栏效果(2)
- 谷普下载 |
| 您所在的位置: >
> 【Android UI设计与开发】第15期:顶部标题栏(六)实现悬浮式顶部和底部标题栏效果(2)【Android UI设计与开发】第15期:顶部标题栏(六)实现悬浮式顶部和底部标题栏效果(2)更新:&&&&编辑:心迹&&&&来源:谷普下载&&&&人气:加载中...&&&&字号:|标签:&&&&&&&&&&&&
&?xml version="1.0" encoding="utf-8"?&
&RelativeLayout xmlns:android="/apk/res/android"
xmlns:tools="/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="/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"
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="/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="/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" &
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" /&
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:
评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)
分类选择您可能在找这些【Android UI设计与开发】第12期:顶部标题栏(三)ActionBar实现层级导航的返回效果 - 推酷
【Android UI设计与开发】第12期:顶部标题栏(三)ActionBar实现层级导航的返回效果
&&&&&&&&&& 今天我们继续来讲解ActionBar的使用,不清楚这个类的读者可以翻阅博主前几篇的文章或者在网络上查阅相关
的资料,关于这个类讲解的文章还是很多的,功能确实也很强大。好的,话不多说,让我们赶快进入正题吧。
一、使用应用图标实现层级导航
&&&&&& 在默认的情况下,应用程序图标显示在操作栏的左边。你能够把这个图标当做操作项来使用,应用程序可以在这
个图标上响应以下两个操作其中之一:
&&&&&&&&1& 返回应用程序的“主”Activity;
&&&&&& &2& 向应用程序上级页面导航。
&要实现应用程序图标能够向上导航,首先就要在你的ActionBar中调用SetDisplayHomeAsUpEnabledtrue(true)方法。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
&&&&&&&当用户触摸这个图标时,系统会调用Activity带有android.R.id.home ID的onOptionsItemSelected()方法。在这个响
应中,你既可以启动主Activity,也可以返回你的应用程序结构化层次中用户上一步操作的界面。
& & & &如果你要通过应用程序图标的响应来返回主Activity,那么就应该在Itent对象中包括
FLAG_ACTIVITY_CLEAR_TOP标识。用这个标记,如果你要启动的Activity在当前任务中已经存在,那么,堆栈中这
个Activity之上的所有的Activity都有被销毁,并且把这个Activity显示给用户。添加这个标识往往是重要的,因为返回主
Activity相当与一个回退的动作,因此通常不应该再创建一个新的主Activity的实例,否则,最终可能会在当前任务中产
生一个很长的拥有多个主Activity的堆栈。
例如,下例的onOptionsItemSelected()方法实现了返回应用程序的主Activity的操作:
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
Intent intent = new Intent(this, HomeActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
return super.onOptionsItemSelected(item);
&&&&&&&&&在用户从另一个应用程序进入当前Activity的情况下,你可能还想要添加
FLAG_ACTIVITY_NEW_TASK
个标识确保在用户返回主页或上级页面时,新的Activity不会被添加到当前的任务中,而是在属于你自己的应用程序的
任务中启动。例如,如果用户通过被另一个应用程序调用的Intent对象启动了你的应用程序中的一个Activity,那么选
择操作栏图标来返回主页或上级页面时,
FLAG_ACTIVITY_CLEAR_TOP
标识会在属于你的应用程序的任务中启动这
个Activity(不是当前任务)。系统既可以用这个新的Activity做根Activity来启动一个新的任务,也可以把存在后台的拥
有这个Activity实例的一个既存任务带到前台来,并且目标Activity会接受
onNewIntent()
回调。因此,如果你的Activity
要接收另一个应用程序的Intent对象,那么通常应该给这个Intent对象添加
FLAG_ACTIVITY_NEW_TASK
标识,如:
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// This is called when the Home (Up) button is pressed
// in the Action Bar.
Intent parentActivityIntent = new Intent(this, MyParentActivity.class);
parentActivityIntent.addFlags(
Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(parentActivityIntent);
return super.onOptionsItemSelected(item);
&&&& 当当前的activity从属于一个不同应用的任务调出时,按下图标按钮应该创建该应用的一个新任务。
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
Intent upIntent = new Intent(this, MyParentActivity.class);
if (NavUtils.shouldUpRecreateTask(this, upIntent)) {
// This activity is not part of the application's task, so create a new task
// with a synthesized back stack.
TaskStackBuilder.from(this)
.addNextIntent(new Intent(this, MyGreatGrandParentActivity.class))
.addNextIntent(new Intent(this, MyGrandParentActivity.class))
.addNextIntent(upIntent)
.startActivities();
// This activity is part of the application's task, so simply
// navigate up to the hierarchical parent activity.
NavUtils.navigateUpTo(this, upIntent);
return super.onOptionsItemSelected(item);
二、在Fragments中实现层级导航
&&&&&&&&当在应用中使用fragments时,单一的FragmentTransaction对象能够表示环境的变化,应该被添加到back堆栈。
例如,如果你要实现一个master/detail流程,通过换出fragments,你应该确保在detail界面上点击Back按钮回退到
master界面。
getFragmentManager().beginTransaction().add(detailFragment, &detail&)
// Add this transaction to the back stack and commit.
.addToBackStack()
.commit();
&&&&&&& 如果FragmentTransaction对象在back堆栈上,activity的FragmentManager会处理Back按钮的点击事件。当这一
事件发生时,FragmentManager从back堆栈中弹出最近一次事务,并进行反向的行为。如果你的应用程序更新其他用
户界面元素来反应当前fragment的状态,例如action bar,记得在commit事务的时候更新UI。
getFragmentManager().addOnBackStackChangedListener(
new FragmentManager.OnBackStackChangedListener() {
public void onBackStackChanged() {
// Update your UI here.
三、导航到外部界面
当启动另一个应用程序的activity来允许用户,写一个邮件,或挑选一个照片附件,你一般不会想让用户在Launcher中重新启动程序时回到这个界面。这会对用户造成混淆。为了阻止这种事情的发生,简单的对intent添加FLAG_ACTIVITY_CLEAR_WITH_TASK_RESET标签来启动外部的activity:
Intent externalActivityIntent = new Intent(Intent.ACTION_PICK);
externalActivityIntent.setType(&image/*&);
externalActivityIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
startActivity(externalActivityIntent);
四、实现的效果图
五、项目结构图
六、详细代码编写
1、这个项目的代码不多,首先是主布局页面,activity_main.xml:
&RelativeLayout xmlns:android=&/apk/res/android&
xmlns:tools=&/tools&
android:layout_width=&match_parent&
android:layout_height=&match_parent&&
android:id=&@+id/other_btn&
android:layout_width=&match_parent&
android:layout_height=&wrap_content&
android:layout_centerHorizontal=&true&
android:layout_marginTop=&160dp&
android:text=&Other_Activity& /&
android:id=&@+id/external_btn&
android:layout_width=&match_parent&
android:layout_height=&wrap_content&
android:layout_alignParentLeft=&true&
android:layout_below=&@+id/other_btn&
android:text=&External_Activity& /&
&/RelativeLayout&
2、然后是另一个界面的布局,activity_other.xml:
&RelativeLayout xmlns:android=&/apk/res/android&
xmlns:tools=&/tools&
android:layout_width=&match_parent&
android:layout_height=&match_parent&&
&/RelativeLayout&
3、主界面Activity类,MainActivity.java:
package com.yangyu.myactionbar04;
import android.app.ActionB
import android.app.A
import android.content.I
import android.os.B
import android.view.V
public class MainActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
* 初始化组件
private void initView(){
final ActionBar actionBar = getActionBar();
actionBar.setHomeButtonEnabled(false);
this.findViewById(R.id.other_btn).setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, OtherActivity.class);
startActivity(intent);
this.findViewById(R.id.external_btn).setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
//调用图片浏览器
Intent externalActivityIntent = new Intent(Intent.ACTION_PICK);
externalActivityIntent.setType(&image/*&);
externalActivityIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
startActivity(externalActivityIntent);
4、进入到另一个Activity界面,OtherActivity.java:
package com.yangyu.myactionbar04;
import android.app.ActionB
import android.content.I
import android.os.B
import android.support.v4.app.FragmentA
import android.support.v4.app.NavU
import android.support.v4.app.TaskStackB
import android.view.MenuI
public class OtherActivity extends FragmentActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_other);
final ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
Intent upIntent = new Intent(this, MainActivity.class);
if (NavUtils.shouldUpRecreateTask(this, upIntent)) {
TaskStackBuilder.from(this)
//如果这里有很多原始的Activity,它们应该被添加在这里
.addNextIntent(upIntent)
.startActivities();
NavUtils.navigateUpTo(this, upIntent);
return super.onOptionsItemSelected(item);
已发表评论数()
已收藏到推刊!
请填写推刊名
描述不能大于100个字符!
权限设置: 公开
仅自己可见
正文不准确
排版有问题
没有分页内容
视频无法显示
图片无法显示}

我要回帖

更多关于 cad标题栏看不见 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信