如何显示在操作栏在Android中右上角的文本? [英] How to show Text at top right corner of Action bar in Android?

查看:126
本文介绍了如何显示在操作栏在Android中右上角的文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的操作栏在我的应用程序的顶部在那里我有要的setTitle谓其示动作条的左上角现在我想告诉一些文本右侧动作条,而不是字幕的最角落

像以红色突出显示要显示在右边一些静态文本 我怎样才能做到这一点?

解决方案

 进口android.app.ActionBar;
进口android.app.Activity;
进口android.os.Bundle;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.View.OnClickListener;
进口android.widget.ImageButton;
进口android.widget.TextView;
进口android.widget.Toast;

公共类MainActivity延伸活动{

    @覆盖
    保护无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);

        动作条mActionBar = getActionBar();
        mActionBar.setDisplayShowHomeEnabled(假);
        mActionBar.setDisplayShowTitleEnabled(假);
        LayoutInflater mInflater = LayoutInflater.from(本);

        查看mCustomView = mInflater.inflate(R.layout.custom_actionbar,NULL);
        TextView的mTitleTextView =(TextView中)mCustomView.findViewById(R.id.title_text);
        mTitleTextView.setText(我自己的标题);



        mActionBar.setCustomView(mCustomView);
        mActionBar.setDisplayShowCustomEnabled(真正的);
    }

}





< XML版本=1.0编码=UTF-8&GT?;
< RelativeLayout的的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =match_parent
    机器人:layout_height =50dp
    机器人:背景=@可绘制/ black_pattern>

    <的TextView
        机器人:ID =@ + ID / title_text
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_centerInParent =真
        机器人:textAllCaps =真
        机器人:textAppearance =机器人:ATTR / textAppearanceLarge
        机器人:文字颜色=#FFF
        机器人:TEXTSTYLE =黑体/>

    < ImageView的
        机器人:ID =@ + ID / imageView1
        机器人:layout_width =35dp
        机器人:layout_height =35dp
        机器人:layout_alignParentLeft =真
        机器人:layout_centerVertical =真
        机器人:layout_marginLeft =8DP
        机器人:SRC =@可绘制/ ic_launcher/>

      <的TextView
        机器人:ID =@ + ID / title_text1
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignParentRight =真
        机器人:layout_centerVertical =真
        机器人:layout_marginRight =8DP
        机器人:textAllCaps =真
        机器人:TEX =356
        机器人:textAppearance =机器人:ATTR / textAppearanceLarge
        机器人:文字颜色=#FFF
        机器人:TEXTSTYLE =黑体/>

< / RelativeLayout的>
 

I am using Action bar at top of my app Where i have setTitle to tht which is shown left corner of ActionBar Now I want to show Some Text To right most corner of the ActionBar instead of subTitle

like highlighted in Red want to show some Static Text at right How can i do this?

解决方案

import android.app.ActionBar;
import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ActionBar mActionBar = getActionBar();
        mActionBar.setDisplayShowHomeEnabled(false);
        mActionBar.setDisplayShowTitleEnabled(false);
        LayoutInflater mInflater = LayoutInflater.from(this);

        View mCustomView = mInflater.inflate(R.layout.custom_actionbar, null);
        TextView mTitleTextView = (TextView) mCustomView.findViewById(R.id.title_text);
        mTitleTextView.setText("My Own Title");



        mActionBar.setCustomView(mCustomView);
        mActionBar.setDisplayShowCustomEnabled(true);
    }

}





<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:background="@drawable/black_pattern" >

    <TextView
        android:id="@+id/title_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:textAllCaps="true"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#fff"
        android:textStyle="bold" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="35dp"
        android:layout_height="35dp"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_marginLeft="8dp"
        android:src="@drawable/ic_launcher" />

      <TextView
        android:id="@+id/title_text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginRight="8dp"
        android:textAllCaps="true"
        android:tex="356"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#fff"
        android:textStyle="bold" />

</RelativeLayout>

这篇关于如何显示在操作栏在Android中右上角的文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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