英文:
Cannot view progressBar created in BaseActivity(parent) in MainActivity(child)?
问题
以下是翻译好的部分:
我正在尝试为所有重复的操作创建一个单一的基本活动。我尝试将进度条放在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);
}
});
}
}
此外,在应用中还有其他内容,但与进度条无关。是的,我已正确声明了按钮和其他内容。
提前感谢!
英文:
I am trying to make a single base activity for all the redundant actions. I tried putting the progress bar in BaseActivity and tried extending it in MainActivity, But it is not showing up at all.
My Code
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);
}
}
}
There are other things in the app, but they have no connection with the progress bar. And yes I have declared the button and other things properly.
Thanks in advance!
答案1
得分: 3
将setContentView()
方法中的以下代码:
super.setContentView(layoutResID);
更改为:
super.setContentView(constraintLayout);
在BaseActivity
中进行这个修改。
英文:
Change
super.setContentView(layoutResID);
to
super.setContentView(constraintLayout);
in setContentView()
method of the BaseActivity.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论