2017-04-07 39 views
0

我试图改变我的行动栏的标题使用其他答案中给出的提示,但他们似乎并没有工作。我试图改变标题为“测试”。为什么我无法更改我的操作栏的标题?

这里是 activity_detectlayout.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" 
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
android:paddingBottom="@dimen/activity_vertical_margin" 
tools:context="com.kaushik.abhishek.facedetecto.detectlayout"> 


<ImageView 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:id="@+id/image_view" 
    android:layout_weight="1.0" 
    android:layout_alignParentTop="true" 
    android:layout_alignParentRight="true" 
    android:layout_alignParentEnd="true" /> 

<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/detect_face" 
    android:text="@string/detect_face" 
    android:layout_gravity="center_horizontal" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentRight="true" 
    android:layout_alignParentEnd="true" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true" 
    android:background="#d4424646" 
    android:textColor="#fdfffa" /> 

detectlayout.java

package com.kaushik.abhishek.facedetecto; 

import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 

public class detectlayout extends AppCompatActivity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    getActionBar().setTitle("Test"); 
    setContentView(R.layout.activity_detectlayout); 
} 

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

} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 

    //noinspection SimplifiableIfStatement 
    if (id == R.id.action_settings) { 
     return true; 
    } 

    return super.onOptionsItemSelected(item); 
} 
} 

清单文件

<?xml version="1.0" encoding="utf-8"?> 

<uses-sdk 
    android:minSdkVersion="8" 
    android:targetSdkVersion="15" /> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 
<uses-permission android:name="android.permission.CAMERA"/> 

<application 
    android:allowBackup="true" 
    android:icon="@mipmap/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 
    <activity 
     android:name=".MainActivity" 
     android:label="@string/app_name" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
    <activity 
     android:name=".detectlayout" 
     android:label="@string/title_activity_detectlayout" > 
    </activity> 
</application> 

styles.xml

<resources> 

<!-- Base application theme. --> 
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> 
    <!-- Customize your theme here. --> 
</style> 

+0

首先证明你的styles.xml,它可能两点原因,如果您使用工具栏一个,你需要setSupportActionBar,或系统中获取的行动吧。 – Remario

+0

我已经尝试过,但我看不到任何变化。 –

+0

尝试从清单文件中的活动中移除android:label =“...”。 –

回答

0

使用此上的OnCreate方法中的活动。

getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
    getSupportActionBar().setTitle("Your Title"); 

或者,通过Menifest

 <activity 
     android:name=".YourActivity" 
     android:label="Your Title" /> 
+0

我试了两次,但没有变化。 –

0

变化getActionBar().setTitle("Test");将其更改为getSupportActionBar instead.Because AppCompatActivity使用支持版本。

如果您使用的是自定义版本。

// Find the toolbar view and set as ActionBar 
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
setSupportActionBar(toolbar); 

// Display icon in the toolbar 
getSupportActionBar().setDisplayShowHomeEnabled(true); 
getSupportActionBar().setLogo(R.mipmap.ic_launcher); 
getSupportActionBar().setDisplayUseLogoEnabled(true); 
+0

您是否定义了任何自定义工具栏?在任何情况下 – Remario

0

试试这个

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    getSupportActionBar().setTitle("your title"); 
    setContentView(R.layout.activity_detectlayout); 
} 
相关问题