2012-08-08 267 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