2012-08-08 144 views
1

我有一个应用程序,它有一个按钮,当它按下时它会触发一个对话框与默认的对话框形状,我想知道我是否可以改变对话框的默认形状为椭圆形,也适用于特殊风格它,android椭圆形状对话框

如下面附着的图像来解释:

1-默认对话框形状:

enter image description here

2,椭圆形对话框形状(我尽量做到):

enter image description here

我dialoge代码:默认对话框的

final Dialog dialog = new Dialog(context); 
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);   
     dialog.setContentView(R.layout.custom_dialog); 

TextView text = (TextView) dialog.findViewById(R.id.dialog_text); 
text.setText(Html.fromHtml(getString(R.string.text_4))); 

ImageView image = (ImageView) dialog.findViewById(R.id.image); 
image.setImageResource(R.drawable.pic); 

Button dialogButton = (Button) dialog.findViewById(R.id.dialog_Button); 

dialogButton.setOnClickListener(new OnClickListener() { 
    public void onClick(View v) { 
     dialog.dismiss(); 
       } 
      }); 

    dialog.show(); 
      } 
     }); 

    } 

风格代码:

<?xml version="1.0" encoding="utf-8"?> 
    <shape xmlns:android="http://schemas.android.com/apk/res/android" 
     android:shape="rectangle"> 
    <stroke android:width="2dp" android:height="2dp" android:color="#B22222" /> 
     <solid android:color="#FCE6C9" /> 
     <padding android:left="2dp" android:top="2dp" android:right="2dp" 
     android:bottom="2dp" /> 
    <corners android:radius="5dp" /> 
    </shape> 

我希望通过代码完成此操作,而不是使用9贴图,所以它会很容易控制dilaoge尺寸和调整里面的文字,因为我neeed,

任何意见将不胜感激,谢谢。

+0

尝试以下添加到您的对话框代码: dialog.getWindow()setBackgroundDrawable(新ColorDrawable(android.graphics.Color.TRANSPARENT))。 – LionKing 2015-02-24 13:53:17

回答

0

不完全知道如何做到这一点在Android,但我在Java中使用的方法是使的JFrame的JDialog渲染的表面透明,然后里面画我自己的自定义形状。使其更加透明化我用AWTUtilities.setWindowOpacity

在这篇文章中,你会发现其他的想法,像捕捉桌面框架或对话框之前被渲染,并用它来修补你的框架:

http://today.java.net/pub/a/today/2008/03/18/translucent-and-shaped-swing-windows.html

的更多信息:

http://java.sun.com/developer/technicalArticles/GUI/translucent_shaped_windows/

这里有一个实现,而不是针对Android,但它可以帮助:

http://www.codeproject.com/KB/java/shaped-transparent-jframe.aspx

0

不错的主意。在Android上,给活动透明背景,把这个在您的清单文件:

<activity android:name=".MyActivity" android:theme="@android:style/Theme.Translucent.NoTitleBar" /> 

然后,在该活动的布局文件,添加此

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" > 

<ImageView 
android:id="@+id/myImageView" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:src="@drawable/myRoundBackground" /> 

<TextView 
android:id="@+id/myImageViewText" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_alignLeft="@+id/myImageView" 
android:layout_alignTop="@+id/myImageView" 
android:layout_alignRight="@+id/myImageView" 
android:layout_alignBottom="@+id/myImageView" 
android:layout_margin="1dp" 
android:gravity="center" 
android:text="Hello" 
android:textColor="#000000" /> 

</RelativeLayout> 

然后你就可以通过编程设置TextView上的文本与id myImageViewText。

+0

你能解释一段代码吗,谢谢 – 2012-08-08 01:38:42

+0

我编辑了我的答案以包含代码。 – 2012-08-13 06:54:24

+0

我应用你的代码,并添加样式对话框,但结果不是我所需要的,即使我不能控制文本位置 – 2012-08-13 19:00:54

0

看看这个quick action menu totorial。尝试应用本教程与形状=椭圆形背景。我想你会得到你想要实现的。


0

我认为一个透明的背景与图像切成椭圆形将为你工作。并将按钮放在椭圆形图像的左侧。

0

该方法类似于您正在尝试的方法: 在您的custom_dialog.xml中,给出相对布局android:background =“@ drawable/ovaldialog”。

而在ovaldialog.xml中,尝试给出相同的参数,通过给出android:shape =“oval”来编辑样式,并给出诸如android:topLeftRadius =“8dp”和android: bottomRightRadius =“10dp”,并使用这些值玩,直到获得所需的形状。为了给对话框赋予红色,给它一个宽度为2/3dp的中风和一个android:color值。

希望这有助于

+0

非常感谢您的提示,它没有给出完整的答案,但直接我来解决我的问题 – 2012-08-16 02:56:27

+0

如果你想我也可以张贴代码,但我想答案是不言自明的。很高兴我能帮上忙 – Android2390 2012-08-16 21:04:36