【问题标题】:Android: How to set different layout title and titlebar icon imageAndroid:如何设置不同的布局标题和标题栏图标图像
【发布时间】:2014-05-09 20:00:16
【问题描述】:

我在 Eclipse Android 应用程序中有 5 个布局视图。我想在所有图形布局中显示不同的布局标题和标题图标图像(在左上角),而不是以编程方式。

目前,它在所有布局中显示相同的应用程序标题,并在所有视图中显示应用程序图标图像。

<application android:name="singleton"
    android:allowBackup="true"
    android:icon="@drawable/icon"
    android:label="@string/app_name"

我尝试了如下登录活动标题标签,login_name 已经添加到字符串中:但是,这里没有显示任何标题。

<activity
        android:name="com.example.myappandroid.LoginActivity"
        android:label="@string/login_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>

请指教,如何通过图形布局解决此问题?

【问题讨论】:

  • 什么样的布局?活动?请注意“操作栏”(显示标题和图标的位置)属于活动。因此,通过 XML,您只能在 / 上分配它们的文本。如果您需要在活动中间更改标题/图标,则必须以编程方式进行。但这真的很简单,只需 getActionBar().setTitle("hi") 将在活动级别执行,或 ((Activity)getContext()).getActionBar().setTitle() 在任何视图级别。或者您可以定义不同的活动,每个活动都有不同的布局、图标和标题
  • 谢谢!我试过 this.setTitle("Login");它正在工作。
  • 非常好!! :) 请注意,如果您执行 this.getActionBar()。 ...您可以在操作栏中更改更多内容:图标、颜色等...
  • 很有用,非常感谢!
  • 不幸的是,如果我像 "this.getActionBar().setTitle("login")" 这样使用它会给出错误,无法构建它。

标签: android


【解决方案1】:

你说你想在操作栏中有你自己的自定义图标和标题吗?如果是,那么我会提到我为此做了什么...... 1.首先我创建一个自定义 xml 文件 2.ActionBar actionBar=getActionBar(); actionBar.setCustomView(R.layout.action_bar);//用这个来加载自己的action bar actionBar.setDisplayShowCustomEnabled(true);

【讨论】:

    【解决方案2】:

    好吧,我最近在我的项目中所做的是..

     <?xml version="1.0" encoding="utf-8"?>//first i created a xml file for the ui i need
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/TopBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#E6E7E9" >
    
        <ImageButton
            android:id="@+id/baricon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:background="@null"
            android:src="@drawable/ic_launcher" />
    
    </RelativeLayout>
    
    ActionBar actionBar=getActionBar();// then i call this part in my activity
    actionBar.setCustomView(R.layout.action_bar);
    actionBar.setDisplayShowCustomEnabled(true);
    enter code here
    

    【讨论】:

      猜你喜欢
      • 2015-02-21
      • 1970-01-01
      • 2011-05-02
      • 1970-01-01
      • 2019-08-26
      • 1970-01-01
      • 2014-12-08
      • 1970-01-01
      • 2016-08-12
      相关资源
      最近更新 更多