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:gravity="start"
android:layout_gravity="start"
android:background="@color/primary"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:popupTheme="@style/ThemeOverlay.AppCompat.Light"
android:elevation="4dp">
<!-- Color is Brown 500 -->
<ProgressBar
android:id="@+id/toolbar_progress_bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
android:indeterminateTint="#795548"
android:indeterminateTintMode="src_in"
android:layout_gravity="right"
/>
</android.support.v7.widget.Toolbar>
<!--android:paddingTop="2dp"/-->
activity_main.xml中
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.widget.DrawerLayout
android:id="@+id/drawerLayout"
android:elevation="7sp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
android:id="@+id/toolbar"
layout="@layout/toolbar_main"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</include>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/container"
android:layout_below="@id/toolbar"/>
</RelativeLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="320dp"
android:background="@android:color/white"
android:layout_height="match_parent"
android:layout_gravity="start"
android:scrollbars="vertical"/>
</android.support.v4.widget.DrawerLayout>
</RelativeLayout>
如何访问工具栏中的ProgressBar并将其设置为我的片段代码中可见?
我试过了:
AppCompatActivity act = (AppCompatActivity) getActivity();
if (act.getSupportActionBar() != null) {
toolbar = (android.support.v7.widget.Toolbar) act.getSupportActionBar().getCustomView();
progress = (ProgressBar)toolbar.findViewById(R.id.toolbar_progress_bar);
}
但是
java.lang.ClassCastException:android.widget.RelativeLayout不能 强制转换为android.support.v7.widget.Toolbar
答案 0 :(得分:3)
你试过
吗? toolbar.findViewById(R.id.toolbar_progress_bar).setVisibility(View.VISIBLE);
对我来说它有效!我在工具栏中有一个ImageView