我想显示工具栏内部包含的进度栏。但是,当我添加layout_gravity =“ bottom”时它不可见,我希望工具栏的进度栏位于底部,就像浏览器一样。 有什么办法可以做到这一点?那Appbarlayout呢?
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"
> <android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:id="@+id/toolbar"
android:layout_height="?attr/actionBarSize"
>
<ProgressBar
android:id="@+id/progressbar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:progressBackgroundTint="@color/colorPrimary"
android:layout_height="3dp"
android:layout_marginBottom="1dp"
android:layout_gravity="bottom"
android:progress="50"
/> </android.support.v7.widget.Toolbar></LinearLayout>
答案 0 :(得分:0)
将此添加到yourstyles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
制作这样的xml文件
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.blogspot.android_er.toolbarprogressbar.MainActivity">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="0dp"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/toolbar"
app:layout_constraintVertical_bias="0.501" />
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="368dp"
android:layout_height="wrap_content"
android:layout_marginLeft="0dp"
android:layout_marginRight="0dp"
android:layout_marginTop="0dp"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="0dp"
android:layout_marginLeft="0dp"
android:layout_marginRight="0dp"
app:layout_constraintBottom_toBottomOf="@+id/toolbar"
app:layout_constraintLeft_toLeftOf="@+id/toolbar"
app:layout_constraintRight_toRightOf="@+id/toolbar" />
</android.support.constraint.ConstraintLayout>
您的活动
public class MainActivity extends AppCompatActivity {
TextView tvTitle;
ProgressBar myProgressBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvTitle = (TextView)findViewById(R.id.title);
myProgressBar = (ProgressBar)findViewById(R.id.progressBar);
tvTitle.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getBaseContext(),
"ProgressBar start running",
Toast.LENGTH_LONG).show();
tvTitle.setClickable(false);
MyAsyncTask myAsyncTask = new MyAsyncTask();
myAsyncTask.execute();
}
});
}
public class MyAsyncTask extends AsyncTask<Void, Integer, Void> {
@Override
protected void onPreExecute() {
myProgressBar.setVisibility(View.VISIBLE);
myProgressBar.setProgress(0);
}
@Override
protected Void doInBackground(Void... voids) {
for(int i=0; i<100; i++){
publishProgress(i);
SystemClock.sleep(100);
}
return null;
}
@Override
protected void onProgressUpdate(Integer... values) {
myProgressBar.setProgress(values[0]);
}
@Override
protected void onPostExecute(Void aVoid) {
myProgressBar.setVisibility(View.GONE);
tvTitle.setClickable(true);
}
}
}
在此演示中,当您单击文本“ Hello world”时,将显示进度对话框
这里是来源的link