android 自訂Dialog背景透明及顯示位置設定

來源:互聯網
上載者:User

標籤:

先貼一下顯示,僅作參考:

 

 

代碼如下:

 

1、自訂Dialog

 

public class SelectDialog extends AlertDialog{

public SelectDialog(Context context, int theme) {
    super(context, theme);
}

public SelectDialog(Context context) {
    super(context);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.slt_cnt_type);
}
}

 

 

2、布局檔案slt_cnt_type.xml代碼 

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:padding="10dp" android:layout_width="115dp" android:layout_height="wrap_content" android:background="@color/blue">
    <Button android:layout_height="wrap_content" android:background="#00000000" android:layout_width="fill_parent" android:text="全部連絡人" android:paddingTop="5dp" android:paddingBottom="5dp" android:paddingLeft="10dp" android:gravity="left|center_vertical" android:id="@+id/btnSltCntAll"></Button>
    <Button android:layout_height="wrap_content" android:background="#00000000" style="@drawable/greenhand_button" android:text="咕咚使用者" android:gravity="left|center_vertical" android:paddingBottom="5dp" android:paddingTop="5dp" android:paddingLeft="10dp" android:paddingRight="10dp" android:layout_width="fill_parent" android:id="@+id/btnSltGudongUser"></Button>
    <Button style="@drawable/greenhand_button" android:background="#00000000" android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="推薦使用者" android:gravity="left|center_vertical" android:paddingTop="5dp" android:paddingBottom="5dp" android:paddingLeft="10dp" android:id="@+id/btnSltRecommend"></Button>
</LinearLayout>

 

3、顏色color.xml代碼

 

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <color name="transparent">#00000000</color>
</resources>

 

4、樣式style.xml代碼

 

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="dialog" parent="@android:style/Theme.Dialog">
        <item name="android:windowFrame">@null</item><!--邊框-->
        <item name="android:windowIsFloating">true</item><!--是否浮現在activity之上-->
        <item name="android:windowIsTranslucent">false</item><!--半透明-->
        <item name="android:windowNoTitle">true</item><!--無標題-->
        <item name="android:windowBackground">@color/transparent</item><!--背景透明-->
        <item name="android:backgroundDimEnabled">false</item><!--模糊-->
    </style>
</resources>

 

 

4、顯示Dialog

 

SelectDialog selectDialog = new SelectDialog(this,R.style.dialog);//建立Dialog並設定樣式主題
Window win = selectDialog.getWindow();
LayoutParams params = new LayoutParams();
params.x = -80;//設定x座標
params.y = -60;//設定y座標
win.setAttributes(params);
selectDialog.setCanceledOnTouchOutside(true);//設定點擊Dialog外部任意地區關閉Dialog
selectDialog.show();

 

 以上僅作參考,如有問題,請大家及時提出來,一起交流學習!

android 自訂Dialog背景透明及顯示位置設定

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.