2017-02-27 84 views
2

我正在尝试显示具有自定义主题的进度条。但它是在左上角而不是中心。如果我不使用自定义主题,它将以中心显示,但不显示全屏,显示文本浏览和其他背景中看起来不好的元素。以下是代码和截图Android:如何在中心显示全屏进度条

自定义主题:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <ProgressBar 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerInParent="true" /> 

</RelativeLayout> 

在活动初始​​化进度与自定义主题对话框:

progressBar = new ProgressDialog(context, R.layout.layout_progress_dialog); 
progressBar.setMessage("Please wait..."); 
progressBar.setCancelable(false); 

enter image description here

当我看到进度条的预览主题单独它显示正确如下。但在活动中,它表现出错误的立场。

enter image description here

+0

这应该已经完成​​了这项工作。你在哪里看到设计选项卡上的进度条? – mehulmpt

+0

@MehulMohan我添加了设计的截图 – codeit

+0

这很奇怪。尝试添加'android:gravity =“center”'到relativelayout – mehulmpt

回答

0

尝试设置android:layout_centerHorizontal="true"android:layout_centerVertical="true"

1

您可以使用<include/>标签进度栏添加到您的活动的布局和设置它的位置。如果您的自定义样式是customProgressDialog.xml

<include 
    layout="@layout/customProgressDialog" 
    android:id="@+id/customProgressDialog" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerInParent="true" 
/> 

然后在您的活动:

ProgressDialog dialog = (ProgressDialog) findViewById(R.id.customProgressDialog); 
0

你传入布局的ID中,需要一个主题的ID的构造。

progressBar = new ProgressDialog(context, R.layout.layout_progress_dialog); 

构造函数是

public ProgressDialog(Context context, int theme) 

你应该定义在style.xml您的自定义属性,并在这里传递风格的ID。