隐藏工具栏中的元素,在单个 activity 上实现,在各种片段上
Hiding elements from toolbar, implemented on a single activity, on various fragments
我试图从我部署在 MainActivity 中的自定义工具栏中隐藏某些元素(它是 我的应用程序中唯一的 activity)。
因此,BotomNavigation 已经实现,它处理 3 个片段,即主页、记录和统计。
主要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"/>
你可以清楚地看到,它有
TextView,动态显示并居中当前用户所在片段的名称和
an 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();
}
}
我要实现的是 隐藏 主页和记录片段上的 ImageView 但是 统计信息。
为此,我在各自的案例陈述中使用了以下内容:
mToolbar.getMenu().findItem(R.id.profileImage).setVisible(false);
但是,由于某种原因,ImageView 保持原样。有趣的是:
1. 占位符图像将继续保留,除非我移动到 Statistics 片段并获取图像。
2. 一旦获取,图像仍然继续存在于工具栏中,即使从统计信息中移出,可能它一开始就没有被隐藏。
3. 如果我旋转成横屏,抓取的图像会被删除,我会再次看到占位符。
我也尝试通过设置 ImageView 本身的可见性来隐藏可见性,但无济于事。
mProfileImageView.setVisibility(View.GONE);
我应该如何真正着手实施正确的逻辑,我是否应该在每个片段中实施工具栏,这肯定不是一种健康的做法并且违背了模块化片段的目的?
以下是一些截图,以便更好地理解:
ImageView not getting hidden, shows placeholder
Is visible even in the Records fragment
placeholder replaced with the user's image (fetched using FirebaseUI Auth)
rotating into landscape switches back to Home fragment and placeholder is replaced with the fetched image
as soon as I switch into statistics, imageview is replaced with the intended image
工具栏内的视图与其他视图相同我尝试更改可见性它有效。
toolbar.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>
我试图从我部署在 MainActivity 中的自定义工具栏中隐藏某些元素(它是 我的应用程序中唯一的 activity)。
因此,BotomNavigation 已经实现,它处理 3 个片段,即主页、记录和统计。
主要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"/>
你可以清楚地看到,它有
TextView,动态显示并居中当前用户所在片段的名称和
an 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();
}
}
我要实现的是 隐藏 主页和记录片段上的 ImageView 但是 统计信息。
为此,我在各自的案例陈述中使用了以下内容:
mToolbar.getMenu().findItem(R.id.profileImage).setVisible(false);
但是,由于某种原因,ImageView 保持原样。有趣的是:
1. 占位符图像将继续保留,除非我移动到 Statistics 片段并获取图像。
2. 一旦获取,图像仍然继续存在于工具栏中,即使从统计信息中移出,可能它一开始就没有被隐藏。
3. 如果我旋转成横屏,抓取的图像会被删除,我会再次看到占位符。
我也尝试通过设置 ImageView 本身的可见性来隐藏可见性,但无济于事。
mProfileImageView.setVisibility(View.GONE);
我应该如何真正着手实施正确的逻辑,我是否应该在每个片段中实施工具栏,这肯定不是一种健康的做法并且违背了模块化片段的目的?
以下是一些截图,以便更好地理解:
ImageView not getting hidden, shows placeholder
Is visible even in the Records fragment
placeholder replaced with the user's image (fetched using FirebaseUI Auth)
rotating into landscape switches back to Home fragment and placeholder is replaced with the fetched image
as soon as I switch into statistics, imageview is replaced with the intended image
工具栏内的视图与其他视图相同我尝试更改可见性它有效。
toolbar.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>