java - Activity ActionBar 在 fragment 中不可见

标签 java android android-fragments android-activity android-fragmentactivity

我的 fragment 中的 ActionBar 有问题。它在其中不可见,因此我无法在 SignUpActivity.java 中执行 getActionBar().setDisplayHomeAsUpEnabled(true); 。在所有其他 Activity(不是 Fragment)中它都是可见的。

你能告诉我我做错了什么吗?可能缺少一些东西。

fragment_sign_up.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="fill_parent"
xmlns:tools="http://schemas.android.com/tools"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
tools:context="com.msh.organizer.SignUpActivity">

<EditText
    android:layout_below="@id/toolbar"
android:id="@+id/nickname_et"
    android:hint="@string/nickname"
    android:layout_marginTop="40dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<EditText
    android:id="@+id/email_et"
    android:hint="@string/mail"
    android:layout_marginTop="5dp"
    android:layout_below="@id/nickname_et"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

<EditText
    android:id="@+id/password_et"
    android:hint="@string/password"
    android:inputType="textPassword"
    android:layout_marginTop="5dp"
    android:layout_below="@id/email_et"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

<Button
    android:id="@+id/sign_up_btn"
    android:layout_marginTop="15dp"
    android:layout_below="@id/password_et"
    android:text="@string/sign_up"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

activity_sign_up.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.msh.organizer.SignUpActivity"
tools:ignore="MergeRootFrame" />

SignUpActivity.java

package com.msh.organizer;

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

public class SignUpActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sign_up);

    //getActionBar().setDisplayHomeAsUpEnabled(true);

    if (savedInstanceState == null) {
        getFragmentManager().beginTransaction()
                .add(R.id.container, new SignUpFragment())
                .commit();
    }
    //Toast.makeText(this,"Sign Up", Toast.LENGTH_LONG).show();
}


@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();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}
}

SignUpFragment.java

package com.msh.organizer;

import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Patterns;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.parse.ParseException;
import com.parse.ParseUser;
import com.parse.SignUpCallback;


public class SignUpFragment extends Fragment implements View.OnClickListener {
private Button mSignUpButton;
private EditText mNickNameEditText;
private EditText mEmailEditText;
private EditText mPasswordEditText;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_sign_up, container, false);

    mSignUpButton = (Button) rootView.findViewById(R.id.sign_up_btn);
    mNickNameEditText = (EditText) rootView.findViewById(R.id.nickname_et);
    mEmailEditText = (EditText) rootView.findViewById(R.id.email_et);
    mPasswordEditText = (EditText) rootView.findViewById(R.id.password_et);

    return rootView;
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    mSignUpButton.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.sign_up_btn:
            trySignUp();
            break;
    }
}

private void trySignUp() {
    String nickname = mNickNameEditText.getText().toString();
    String email = mEmailEditText.getText().toString();
    String password = mPasswordEditText.getText().toString();
    boolean failed = false;

    if (TextUtils.isEmpty(nickname)){
        mNickNameEditText.setError("Nickname cannnot be empty!");
        failed = true;
    }

    if (TextUtils.isEmpty(password)){
        mPasswordEditText.setError("Password cannnot be empty!");
        failed = true;
    }

    if (TextUtils.isEmpty(email)){
        mEmailEditText.setError("Email cannnot be empty!");
        failed = true;
    }

    if (!Patterns.EMAIL_ADDRESS.matcher(email).matches()){
        mEmailEditText.setError("Inwalid E-mail address!");
        failed = true;
    }
    if (!failed){
        signUp(nickname, email, password);
    }

}

private void signUp(String nickname, String email, String password) {
    ParseUser user = new ParseUser();
    user.setUsername(email);
    user.setPassword(password);
    user.setEmail(email);

// other fields can be set just like with ParseObject
    user.put("nickname", nickname);

    user.signUpInBackground(new SignUpCallback() {
        public void done(ParseException e) {
            if (e == null) {
                if (getActivity() != null){
                    getActivity().setResult(Activity.RESULT_OK);
                    getActivity().finish();
                }
            } else {
                if (getActivity() != null) {
                    Toast.makeText(getActivity(), e.getLocalizedMessage(),     Toast.LENGTH_LONG).show();
                }
            }
        }
    });
}
}

最佳答案

您可以在 Activity 的 AppBarLayout 内使用 Toolbar:

SignUpActivity.java

    ...

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    final ActionBar ab = getSupportActionBar();
    ab.setHomeAsUpIndicator(R.drawable.ic_menu);
    ab.setDisplayHomeAsUpEnabled(true);

    ...

activity_sign_up.xml

<android.support.design.widget.AppBarLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        app:layout_scrollFlags="scroll|enterAlways|snap" />

</android.support.design.widget.AppBarLayout>

<FrameLayout 
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.msh.organizer.SignUpActivity"
    tools:ignore="MergeRootFrame" />

关于java - Activity ActionBar 在 fragment 中不可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38595496/

相关文章:

android - FFmpeg - 我如何获得打开 ALAC 编解码器所需的额外数据?

android - ViewPager View 未显示

java - 将字符串拆分为特定大小的 String[]

java - 在 Java 中使用什么数据类型来赚钱?

java - 手动触发 EJB Schedule 进行测试

java - 使用oracle java在jenkins中构建android项目

java - 将大写日期解析为 LocalDate

android - 通过处理程序的无限循环会阻塞 UI 线程吗?

java - Fragment 中的 RecyclerView

java - Android - 未找到嵌套 fragment View