繁体   English   中英

隐藏工具栏上的元素(在单个活动上,在各种片段上实现)

[英]Hiding elements from toolbar, implemented on a single activity, on various fragments

我正在尝试从自定义工具栏中隐藏某些元素,这些元素已部署在MainActivity中(这是我应用程序中唯一的活动 )。

因此,已经实现了处理3个片段的 BotomNavigation, 即Home,Records和Statistics

主要的toolbar.xml组成如下:

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

<android.support.v7.widget.Toolbar 
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
android:elevation="4dp">

<TextView
    android:id="@+id/toolbarTitle"
    style="@style/TextAppearance.AppCompat.Widget.ActionBar.Title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />

<ImageView
    android:id="@+id/profileImage"
    android:layout_width="25dp"
    android:layout_height="25dp"
    android:src="@drawable/ic_account_circle_white_50dp"/>

如您所见,它具有

  1. TextView ,它动态显示当前用户所在的片段名称并将其居中

  2. 一个ImageView ,显示经过身份验证的用户的图像,其中,如果用户没有个人资料图片,则src只是一个占位符。

使用以下MainActivity.java,并非一切都能按预期工作:

public class MainActivity extends AppCompatActivity {

private TextView mTitle;
private String mUsername, mEmail;
private Uri mProfileImage;
private ImageView mProfileImageView;
private android.support.v7.widget.Toolbar mToolbar;
private static final String TAG = "BottomNavigation";

private BottomNavigationView.OnNavigationItemSelectedListener navListener;

public BottomNavigation() {

    navListener = new BottomNavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            Fragment fragment;

            switch (item.getItemId()) {
                case R.id.nav_home:
                    mTitle.setText("Home");
            mToolbar.getMenu().findItem(R.id.profileImage).setVisible(false);
                    fragment = new HomeFragment();
                    loadFragment(fragment);
                    break;

                case R.id.nav_records:
                    mTitle.setText("Records");
            mToolbar.getMenu().findItem(R.id.profileImage).setVisible(false);
                    fragment = new RecordsFragment();
                    loadFragment(fragment);
                    break;

                case R.id.nav_stats:
                    mTitle.setText("Statistics");
                    fragment = new StatisticsFragment();
                    loadFragment(fragment);
                    Glide.with(getApplicationContext()).load(mProfileImage).into(mProfileImageView);
                    break;
            }

            return true;
        }
    };
}

}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_bottom_naviation);

    //replacing the default actionbar with custom toolbar
    mToolbar = findViewById(R.id.tool_bar);
    setSupportActionBar(mToolbar);
    mTitle = mToolbar.findViewById(R.id.toolbarTitle);
    //disabling the output of the appname on the toolbar
    getSupportActionBar().setDisplayShowTitleEnabled(false);


    BottomNavigationView bottomNav = findViewById(R.id.bottom_navigation);
    bottomNav.setOnNavigationItemSelectedListener(navListener);


    //load the Home fragment by default

    loadFragment(new HomeFragment());
    mTitle.setText("Home");


    FirebaseUser currentUser = FirebaseAuth.getInstance().getCurrentUser();
            mProfileImage = currentUser.getPhotoUrl();
    mProfileImageView = mToolbar.findViewById(R.id.profileImage);

}

private void loadFragment(Fragment fragment) {
    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    transaction.replace(R.id.fragment_container, fragment);
    transaction.commit();
}
}

我要实现的是在Home和Record片段上隐藏 ImageView, 但在 Statistics上。

为此,我在相应的case语句中使用了以下内容:

mToolbar.getMenu().findItem(R.id.profileImage).setVisible(false);

但是,由于某些原因,ImageView保持原样。 有趣的是:

1.占位符图像将继续保留,直到并且除非我移至“统计信息”片段并获取图像。

2.提取后,即使从统计信息中移出图像,该图像仍将继续存在于工具栏中,可能它并没有被隐藏。

3.如果我旋转到风景中,则所获取的图像将被删除,并且再次出现占位符。

我还尝试通过将可见性设置为ImageView本身来隐藏可见性,但无济于事。

mProfileImageView.setVisibility(View.GONE);

我该如何实际地实施正确的逻辑,是否应该在每个片段中都使用工具栏,这肯定不是健康的做法,并且违反了模块化片段的目的?

以下是一些屏幕截图,以便更好地理解:

ImageView没有隐藏,显示占位符

即使在“记录”片段中也可见

占位符替换为用户的图像(使用FirebaseUI Auth获取)

旋转为横向切换回首页片段,并将占位符替换为获取的图像

一切换到统计信息,imageview就会替换为预期的图像

工具栏内的视图与我尝试更改其可见性的其他视图相同。

工具栏.findViewById(R.id.image).setVisibility(View.VISIBLE);

public class TestActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);
        final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        toolbar.findViewById(R.id.image).setVisibility(View.GONE);
        toolbar.postDelayed(new Runnable() {
            @Override
            public void run() {

  toolbar.findViewById(R.id.image).setVisibility(View.VISIBLE);
            }
        }, 5000);
    }
}

我的工具栏xml

<android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/holo_blue_light"
            android:contentInsetEnd="0dp"
            android:contentInsetLeft="0dp"
            android:contentInsetRight="0dp"
            android:contentInsetStart="0dp"
            app:contentInsetEnd="0dp"
            app:contentInsetLeft="0dp"
            app:contentInsetRight="0dp"
            app:contentInsetStart="0dp">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <ImageView
                    android:id="@+id/image"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentStart="true"
                    android:layout_marginRight="10dp"
                    android:src="@drawable/ic_launcher_foreground" />

                <TextView
                    android:id="@+id/appTitle"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:layout_centerInParent="true"
                    android:gravity="center"
                    android:text="Title"
                    android:textColor="@android:color/background_dark"
                    android:textSize="20sp" />


            </RelativeLayout>

        </android.support.v7.widget.Toolbar>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM