Android 进度条未显示在屏幕中,但显示在调试器中
当用户进行搜索查询时,我添加了一个进度条,它不会显示在屏幕上。以下是片段:Android 进度条未显示在屏幕中,但显示在调试器中,android,Android,当用户进行搜索查询时,我添加了一个进度条,它不会显示在屏幕上。以下是片段: protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Button searchButton = (Button) findViewById(R.id.searchButton); setContentView(R.layout.activit
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Button searchButton = (Button) findViewById(R.id.searchButton);
setContentView(R.layout.activity_main); //main layout, which has a child layout called resultsLayout
searchButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
EditText searchString = (EditText) findViewById(R.id.searchString);
search(searchString.getText().toString());
}
});
protected void search(String searchString){
LinearLayout resultLayout = ((LinearLayout) findViewById(R.id.resultsLayout));
resultLayout.addView(DisplayResults.getLoadBar(getApplicationContext()));
//do search
resultLayout.removeAllViews();
}
public static ProgressBar getLoadBar(Context context){
ProgressBar loadBar = new ProgressBar(context);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
loadBar.setLayoutParams(params);
loadBar.setVisibility(View.VISIBLE);
return loadBar;
}
我知道其中一些可能不需要,比如在创建时正确设置可见性,但我尝试了几种方法,但都不起作用。也许没有足够的时间显示该栏?(搜索大约需要1.5秒)
我还调试了应用程序,它将ProgressBar显示为布局的子级,但我仍然无法在屏幕上看到它
您可以尝试此ProgressDialog对象实现。
添加
设置内容视图(resultLayout)
在search()
method的末尾,我意识到android不会为代码中的每一条指令更新屏幕,显然它只会在函数结束时更新屏幕。也就是说,我必须在一个单独的线程中进行搜索,以便在搜索开始到结束之间显示加载。不应该是这样。我从onCreate中取出了一些东西,我将添加它们以明确说明问题。如果您不能说函数已弃用,那么请实际添加所有这些东西,但我仍然无法执行我最初想要的操作。说哪个函数已弃用?你能贴张照片吗?那没问题。你仍然可以使用它。
ProgressDialog progressDialog; //Create a prog dialog object
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Button searchButton = (Button) findViewById(R.id.searchButton);
progressDialog=new ProgressDialog(this); //init it
searchButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
progressDialog.show(); //show it when needed
EditText searchString = (EditText) findViewById(R.id.searchString);
search(searchString.getText().toString());
}
});
protected void search(String searchString){
progressDialog.dismiss();//dismiss it when the search is complete
LinearLayout resultLayout = ((LinearLayout) findViewById(R.id.resultsLayout));
resultLayout.addView(DisplayResults.getLoadBar(getApplicationContext()));
//do search
resultLayout.removeAllViews();
}