本方法中,一定要在 sytle 文件加入如下代码,否则启动后会抛出异常:
<item name="windowActionBar">true</item>
这是因为 AppCompatActivity 的默认 style 中含有如下设置:
<item name="windowActionBar">false</item>
调整之后的 style 代码像下面的样子:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="windowNoTitle">true</item>
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
</resources>
style 代码部分我们可以根据自己的实际情况调整,就不再赘述。
接下来是 自定义 toolbar 的布局文件:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ProgressBar
android:id="@+id/progress_circular"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:indeterminate="true"
android:visibility="gone" />
<LinearLayout
android:id="@+id/progress_horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:visibility="gone"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/app_name"
android:layout_gravity="center_vertical"
style="@style/TextAppearance.Widget.AppCompat.Toolbar.Title"
/>
<ProgressBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:indeterminate="true"
style="@style/Widget.AppCompat.ProgressBar.Horizontal"
/>
</LinearLayout>
</android.support.v7.widget.Toolbar>
主布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include layout="@layout/toolbar"/>
<Button
android:id="@+id/change_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/horizontal"
android:layout_marginTop="40dp"
android:layout_gravity="center_horizontal"
android:onClick="onClicked"
/>
</LinearLayout>
主程序代码:
package com.toby.personal.testlistview;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
public class MainActivity extends AppCompatActivity {
final private static String TAG = "Toby_Test";
private ProgressBar progress_circular = null;
private LinearLayout progress_horizontal = null;
private Button change_button = null;
private boolean CircularEnabled = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
progress_circular = (ProgressBar) findViewById(R.id.progress_circular);
progress_horizontal = (LinearLayout) findViewById(R.id.progress_horizontal);
change_button = (Button) findViewById(R.id.change_button);
progress_circular.setVisibility(View.VISIBLE);
}
public void onClicked(View view) {
if (CircularEnabled) {
progress_circular.setVisibility(View.GONE);
progress_horizontal.setVisibility(View.VISIBLE);
change_button.setText(R.string.horizontal);
} else {
progress_horizontal.setVisibility(View.GONE);
progress_circular.setVisibility(View.VISIBLE);
change_button.setText(R.string.circular);
}
CircularEnabled = !CircularEnabled;
}
}
启动之后的显示效果:
首次启动运行效果
点击按钮之后的运行效果:
点击按钮之后的运行效果
网友评论