【问题标题】:Action bar not showing on new activity新活动上未显示操作栏
【发布时间】:2014-12-10 17:14:25
【问题描述】:

新活动的操作栏未显示。我花了几个小时在 stack over flow 和其他网站上寻找可能的原因,但我无法找出问题所在。

您能告诉我可能是什么原因以及可能的解决方案吗?

以下是我的代码

首先我创建了:activity_forgot_password.xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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:background="#ffffff" 
>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context="com.reach2employee.ForgotPasswordActivity"
        >

            <ImageView
               android:id="@+id/appLogo"
               android:layout_width="fill_parent" 
               android:layout_height="wrap_content"
               android:contentDescription="@string/app_name"
               android:src="@drawable/onlyitemployees_logo"
            />

            <TextView
                android:id="@+id/forgotPasswordTextView"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:textSize="20sp"
                android:text="@string/forgotPasswordBtnText"
                android:textColor="#D08B3A"
                android:layout_marginTop="15dp" 
                />                

            <EditText
                android:id="@+id/employerUsername"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" 
                android:hint="@string/employerUsernameHint"
                android:layout_marginTop="15dp"
                />


            <Button 
                android:id="@+id/submitBtn"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:background="#8eb534"
                android:textColor="#ffffff"
                android:text="@string/submitBtn"
                android:layout_marginTop="15dp"
                style="?android:attr/borderlessButtonStyle"
                />                                       

</LinearLayout>


</ScrollView>

其次我创建了 ForgotPasswordActivity.java

public class ForgotPasswordActivity extends Activity {

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);     
        setContentView(R.layout.activity_forgot_password);  
    }       

}

第三,我在清单文件中添加了一些代码

<activity 
        android:name=".ForgotPasswordActivity"
        android:label="Forgot Password"
        ></activity>

【问题讨论】:

  • 如果您不提供任何代码,我们无法告诉您原因。
  • 好的,我为您提供代码。只需一分钟
  • 检查:新活动的基类,新活动的主题
  • 也许你正在使用像“Theme.Sherlock.Light.NoActionBar”这样的主题,它使ActionBar不可见,但我同意Tanis.7x,没有任何代码很难说
  • @Tanis.7x 我更新了我的帖子。请立即查看。

标签: android android-activity android-actionbar


【解决方案1】:

假设您正在尝试获取本机 android 操作栏而不是 sherlok 的.....您的活动是否扩展ActionBarActivity?没有那个不可能。更正基类后,确保您的活动使用支持操作栏的主题。

【讨论】:

    【解决方案2】:

    在我的第二个 Activity 中,我不得不再次处理 onCreateOptionsMenu() 处理程序:

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.topmenu, menu);
    
        return true;
    }
    

    还有菜单项的事件处理程序:

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
    
            case R.id.menu_home:
                gotohome();
                return true;
            case R.id.menu_admin:
                gotoadmin();
                return true;
            default:
                return super.onOptionsItemSelected(item);
    
        }
    }
    

    更新:我决定创建一个基本活动类,我的所有活动都将从该类继承。我把所有的共享代码都放在那里了。

    【讨论】:

      【解决方案3】:

      我也遇到过类似的问题。我的班级正在扩展“Activity”而不是“ActionBarActivity”。在我扩展“ActionBarActivity”后,Action Bar 出现了。

      【讨论】:

        猜你喜欢
        • 2015-08-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-10-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多