活动的机器人操作栏中中心称号 [英] Center title of activity in android action bar

查看:189
本文介绍了活动的机器人操作栏中中心称号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在我的活动标题显示,以左侧为<标题,然后其他的菜单项显示在右边。我想我的居中标题和离开了< 。我怎么做?我使用典型的菜单我称之为使用

 公共布尔onOptionsItemSelected(菜单项项)
 

  @覆盖
公共布尔onCreateOptionsMenu(功能菜单){
    //充气菜单;这增加了项目操作栏,如果它是present。
    。getMenuInflater()膨胀(R.menu.yesterday,菜单);
    返回true;
}
 

编辑:

我设法得到它使用显示

  requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
    的setContentView(R.layout.activity_yesterday);
    。getWindow()setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.yesterday_title_bar);
 

其中, yesterday_title_bar 是一个RelativeLayout的

但它显示的看法高度的一半。所以,我创建了以下的风格。但是,当我应用该样式的清单。该清单说,它无法找到资源。

 <样式名称=CustomWindowTitleBackground>
        <项目名称=机器人:背景>#323331< /项目>
    < /风格>

    <样式名称=CustomTheme父=安卓主题>
        <项目名称=机器人:windowTitleSize> 65dip< /项目>
        <项目名称=机器人:windowTitleBackgroundStyle> @风格/ CustomWindowTitleBackground< /项目>
    < /风格>
 

清单:

 <活动
        机器人:名称=com.company.YesterdayActivity
        机器人:主题=@安卓风格/ CustomTheme>
    < /活性GT;
 

解决方案

我还用动作条福尔摩斯和我设置页眉位置用这种方法。您可以设置自定义的TextView,并可以设置它的引力中心。使用下面的方法,并告诉我。

 私人无效SetHeader可以(){
    LayoutInflater linflater = (LayoutInflater)MainAccountActivity.context.getSystemService(MainAccountActivity.context.LAYOUT_INFLATER_SERVICE);
    查看GalleryTitleView = linflater.inflate(R.layout.layout_header,NULL);

    galleryTitleTv =(TextView中)GalleryTitleView
            .findViewById(R.id.GalleryTitleTv);
    galleryTitleTv.setText(ITWAppUIConstants.TAB_AGENDA);

    ActionBar.LayoutParams LP =新ActionBar.LayoutParams(
            ActionBar.LayoutParams.FILL_PARENT,
            ActionBar.LayoutParams.WRAP_CONTENT);
    lp.gravity = Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL;
    GalleryTitleView.setLayoutParams(LP);
    getSupportActionBar()setCustomView(GalleryTitleView)。
  }
 

和这里是我使用的布局:

 < XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:背景=@色/透明
机器人:重力=中心
机器人:方向=垂直>

<的TextView
    机器人:ID =@ + ID / GalleryTitleTv
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =WRAP_CONTENT
    机器人:重力=中心
    机器人:以下属性来=10dip
    机器人:文字颜色=@色/白
    机器人:TEXTSIZE =20dip
    机器人:TEXTSTYLE =黑体/>
 

Right now the title of my activity shows to the left as < Title and then the other menu item shows to the right. I want to center my title and leave out the <. How do I do that? I am using typical menu which I call using

public boolean onOptionsItemSelected(MenuItem item) 

and

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.yesterday, menu);
    return true;
}

EDIT:

I managed to get it to show using

requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
    setContentView(R.layout.activity_yesterday);
    getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,   R.layout.yesterday_title_bar);

where yesterday_title_bar is a relativeLayout

But it's showing half the height of the views. So I created the following style. But when I apply the style in the manifest. The manifest says it cannot find the resource.

<style name="CustomWindowTitleBackground">
        <item name="android:background">#323331</item>
    </style>

    <style name="CustomTheme" parent="android:Theme">
        <item name="android:windowTitleSize">65dip</item>
        <item name="android:windowTitleBackgroundStyle">@style/CustomWindowTitleBackground</item>
    </style>

Manifest:

<activity
        android:name="com.company.YesterdayActivity"
        android:theme="@android:style/CustomTheme" >
    </activity>

解决方案

I also used actionbar sherlock and i set header position by this method. you can set a custom textview and can set its gravity to center. use the below method and tell me.

    private void setHeader(){
    LayoutInflater linflater = (LayoutInflater)MainAccountActivity.context.getSystemService(MainAccountActivity.context.LAYOUT_INFLATER_SERVICE);
    View GalleryTitleView = linflater.inflate(R.layout.layout_header, null);

    galleryTitleTv = (TextView) GalleryTitleView
            .findViewById(R.id.GalleryTitleTv);
    galleryTitleTv.setText(ITWAppUIConstants.TAB_AGENDA);

    ActionBar.LayoutParams lp = new ActionBar.LayoutParams(
            ActionBar.LayoutParams.FILL_PARENT,
            ActionBar.LayoutParams.WRAP_CONTENT);
    lp.gravity = Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL;
    GalleryTitleView.setLayoutParams(lp);
    getSupportActionBar().setCustomView(GalleryTitleView);
  }

and here is the layout i used:

  <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@color/transparent"
android:gravity="center"
android:orientation="vertical" >

<TextView
    android:id="@+id/GalleryTitleTv"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:paddingLeft="10dip"
    android:textColor="@color/white"
    android:textSize="20dip"
    android:textStyle="bold" />

这篇关于活动的机器人操作栏中中心称号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆