【问题标题】:How to style android default components如何样式化android默认组件
【发布时间】:2012-09-27 15:28:25
【问题描述】:

我的应用程序一启动,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();
}

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

(有些样式是继承自 Widget.Sherlock.Light.ProgressBar)

到目前为止一切顺利。使用颜色文本属性,我将其更改为红色,但这就是我所能做的。 问题是我想为标题文本、消息文本和蓝色线条赋予样式。

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

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 在代码中访问此视图并将我希望的样式应用于它们。 p>

我知道如何进行自定义布局,但我想知道是否有一些方法可以设置默认组件的样式

谢谢

【问题讨论】:

    标签: android android-alertdialog progressdialog


    【解决方案1】:

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

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

    【讨论】:

    • 我没有使用 AlertDialog,我使用的是 ProgressDialog。我已经阅读了this,但感谢您的复制粘贴。
    【解决方案2】:

    研究了一段时间后,我认为这是不可能的。默认组件的样式是使用您的应用程序无法访问的 id 设置的,这使得它不可行,除非您创建自己的自定义组件。

    【讨论】:

      猜你喜欢
      • 2021-03-03
      • 2015-06-26
      • 2016-12-14
      • 2022-06-18
      • 2012-10-24
      • 1970-01-01
      • 1970-01-01
      • 2019-11-05
      • 1970-01-01
      相关资源
      最近更新 更多