android - 在操作栏中添加 TextView - android

标签 android

我想在操作栏的右上角显示文本。我已使用以下布局将 TextView 添加到操作栏。

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:background="#00000000"
android:layout_gravity="right"
android:gravity="right"
android:layout_height="fill_parent">

<TextView
    android:id="@+id/balance_text"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginRight="8dp"
    android:textAllCaps="true"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textColor="#fff"
    android:layout_gravity="center_vertical|right"
    android:gravity="right"
    android:text="test balance"
    android:textStyle="bold" />

在 Activity 中为

    LayoutInflater inflator = LayoutInflater.from(this);
    View v = inflator.inflate(R.layout.actionbar_layout, null);
    ((LinearLayout) v).setGravity(Gravity.RIGHT);

    actionBar.setCustomView(v);

它工作得很好。但它会在操作栏的中心添加文本。我希望将文本放在右侧。我也尝试过重力。但没有任何效果。

任何建议。

最佳答案

第一步,您必须创建一个布局

<?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" />

<ImageButton
    android:id="@+id/imageButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_centerVertical="true"
    android:layout_marginRight="8dp"
    android:background="@null"
    android:src="@android:drawable/ic_menu_rotate" />

在 onCreate 中:

    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");

    ImageButton imageButton = (ImageButton) mCustomView
            .findViewById(R.id.imageButton);
    imageButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View view) {
            Toast.makeText(getApplicationContext(), "Refresh Clicked!",
                    Toast.LENGTH_LONG).show();
        }
    });

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

关于android - 在操作栏中添加 TextView - android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29153099/

相关文章:

android - 如何关闭 AlertDialog.Builder?

java - 无法比较类型错误 : Object and int in Telegram

android - DLL 加载失败 : %1 is not a valid Win32 application - Appcelerator

javascript - 使用phonegap获取文件夹内容(android)

java - 集中底部导航点击事件代码

java - Android - 通过单个类填充多个 ListView

javascript - 如何在 Cordova 中播放音频文件后打开页面?

java - 将按钮高度和宽度设置为包装内容并填充父级

android - 免费向现有客户分发付费应用程序

Android 支持库 appcompat v7 : cannot find actionModeShareDrawable resource