如何在Android中更改自定义对话框的标题栏高度

3

在我的安卓应用中,我有一个自定义对话框。我想设置对话框标题栏的高度。我的样式如下:

<resources>
    <style name="customDialogStyle" parent="android:Theme.Dialog"> 
        <item name="android:background">#04a9ee</item>
        <item name="android:height">5dp</item> 
     </style> 
</resources>

但是"title"栏的高度并没有受到"height"属性的影响。那么如何改变自定义对话框标题栏的高度呢?
2个回答

2

这对我有效

您的主题:

  <resources>
       <style name="MyDialog" parent="android:Theme.Holo.Dialog">
           .......

       </style>

  </resources>

您的自定义对话框类:

  public class CustomDialog extends Dialog
  {
       public CustomDialog(Context context, int theme) {
           super(context, theme);
       }


       @Override
       protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);

          ........... 

          Resources res = getContext().getResources();
          int titleId = res.getIdentifier("title", "id", "android");
          View title = findViewById(titleId);
          if (title != null) {
              title.getLayoutParams().height = 5; // your height
          }
       }
  }

创建对话框并在代码中显示:

   CustomDialog customDialog = new CustomDialog(this, R.style.MyDialog);
   customDialog.show();

2

我刚刚检查了一下,你想使用 "android:layout_height",其他高度也可以使用,例如:"android:minHeight"


我已经尝试了 "android:layout_height" 和 "android:minHeight" 但是标题栏的大小没有改变。这些属性会影响对话框或其标题栏吗?在我的情况下什么也没有发生。 - rizzz86
2
你无法在对话框中本地更改标题栏的高度,你必须创建自己的视图并将对话框的视图设置为你的自定义视图。可以像这样完成:http://developer.android.com/guide/topics/ui/dialogs.html#CustomDialog - SnowyTracks

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接