无法在 MainActivity(child) 中查看在 BaseActivity(parent) 中创建的 progressBar?
Cannot view progressBar created in BaseActivity(parent) in MainActivity(child)?
我正在尝试为所有冗余操作创建一个基础 activity。我尝试将进度条放在 BaseActivity 中并尝试在 MainActivity 中扩展它,但它根本没有显示出来。
我的代码
activity_base.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/activity_content"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
<ProgressBar
android:id="@+id/progressBar"
android:indeterminate="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
BaseActivity.java
public abstract class BaseActivity extends AppCompatActivity {
private static final String TAG = "BaseActivity";
public ProgressBar progressBar;
@Override
public void setContentView(int layoutResID) {
ConstraintLayout constraintLayout = (ConstraintLayout) getLayoutInflater().inflate(R.layout.activity_base, null);
FrameLayout frameLayout = constraintLayout.findViewById(R.id.activity_content);
progressBar = constraintLayout.findViewById(R.id.progressBar);
Log.i(TAG, "setContentView: Progressbar" + progressBar.getVisibility());
getLayoutInflater().inflate(layoutResID, frameLayout, true);
super.setContentView(layoutResID);
}
public void showProgressBar(boolean visibility) {
progressBar.setVisibility(visibility ? View.VISIBLE : View.GONE);
}
}
MainActivity.java
public class MainActivity extends BaseActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_get_data = (Button) findViewById(R.id.btn_get_data);
btn_get_data.setOnClickListener(view -> {
if (progressBar.getVisibility() == View.VISIBLE) {
Log.i(TAG, "onCreate: DISABLING PROGRESS BAR");
showProgressBar(false);
} else {
showProgressBar(true);
}
}
}
app里面还有其他东西,但是跟进度条没有关系。是的,我已经正确地声明了按钮和其他东西。
提前致谢!
改变
super.setContentView(layoutResID);
至
super.setContentView(constraintLayout);
在 BaseActivity 的 setContentView()
方法中。
我正在尝试为所有冗余操作创建一个基础 activity。我尝试将进度条放在 BaseActivity 中并尝试在 MainActivity 中扩展它,但它根本没有显示出来。 我的代码
activity_base.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/activity_content"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
<ProgressBar
android:id="@+id/progressBar"
android:indeterminate="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
BaseActivity.java
public abstract class BaseActivity extends AppCompatActivity {
private static final String TAG = "BaseActivity";
public ProgressBar progressBar;
@Override
public void setContentView(int layoutResID) {
ConstraintLayout constraintLayout = (ConstraintLayout) getLayoutInflater().inflate(R.layout.activity_base, null);
FrameLayout frameLayout = constraintLayout.findViewById(R.id.activity_content);
progressBar = constraintLayout.findViewById(R.id.progressBar);
Log.i(TAG, "setContentView: Progressbar" + progressBar.getVisibility());
getLayoutInflater().inflate(layoutResID, frameLayout, true);
super.setContentView(layoutResID);
}
public void showProgressBar(boolean visibility) {
progressBar.setVisibility(visibility ? View.VISIBLE : View.GONE);
}
}
MainActivity.java
public class MainActivity extends BaseActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_get_data = (Button) findViewById(R.id.btn_get_data);
btn_get_data.setOnClickListener(view -> {
if (progressBar.getVisibility() == View.VISIBLE) {
Log.i(TAG, "onCreate: DISABLING PROGRESS BAR");
showProgressBar(false);
} else {
showProgressBar(true);
}
}
}
app里面还有其他东西,但是跟进度条没有关系。是的,我已经正确地声明了按钮和其他东西。
提前致谢!
改变
super.setContentView(layoutResID);
至
super.setContentView(constraintLayout);
在 BaseActivity 的 setContentView()
方法中。