如何设置android默认组件的样式

时间:2012-09-27 15:28:25

标签: android alertdialog progressdialog

我的应用启动后,AsyncTask会检索JSON。在下载时,我会像往常一样显示进度对话框:

protected void onPreExecute() {
    Context c = ((EventoMainActivity)mCallback);
    mDialog = new ProgressDialog(c, R.style.my_Dialog);
    mDialog.setTitle("Descargando datos del evento ");
    mDialog.setMessage("Espere un instante...");
    mDialog.setIndeterminate(true);
    mDialog.show();
}

由于我想自定义对话框,我使用构造函数传递我的样式以更改颜色等,从而实现以下结果:

enter image description here

(某些样式来自Widget.Sherlock.Light.ProgressBar的继承)

到目前为止一切顺利。使用颜色文本属性我将其更改为红色,但这是我设法做的全部。 问题在于我想为标题文字,邮件文本和蓝色线条赋予样式。

我的方法是在android源代码中查找这些元素的结构(它们的布局xml文件)。我发现progressDialog来自alertDialog。在那里我找到了定义title参数和消息参数的结构:

From alert_dialog.xml
...
<com.android.internal.widget.DialogTitle android:id="@+id/alertTitle"
                style="?android:attr/textAppearanceLarge"
                android:singleLine="true"
                android:ellipsize="end"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
...

From progress_dialog.xml
...    
<TextView android:id="@+id/message"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical" />
...

问题是 android.R.id不包含对这些ID的引用,因此我无法在使用findByID的代码中访问此视图并向其应用样式I愿望。

我知道如何进行自定义布局,但 我想知道是否有某种方式来设置默认组件的样式

由于

2 个答案:

答案 0 :(得分:0)

您可以使用包装器,您可以在其中为当前上下文中的任何内容设置主题(样式)。您可以将R.style.MyTheme设置为警告对话框的样式,并根据自己的喜好自定义该视图。

ContextThemeWrapper ctw = new ContextThemeWrapper( this, R.style.MyTheme );
AlertDialog.Builder builder= new AlertDialog.Builder( ctw );

答案 1 :(得分:0)

经过一段时间的研究,我认为这是不可能的。默认组件使用无法从您的应用程序访问的ID进行样式设置,这使得除非您创建自己的自定义组件,否则无法执行此操作。

相关问题