MainActivity如下:
package cn.com; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup.LayoutParams; import android.widget.Button; import android.app.Activity; import android.app.Dialog; /** * Demo描述: * 去掉自定义Dialog的黑色边框 * 主要手段: * 在生成Dialog的时候为其指定一个style * 请参见styles.xml文件 * */ public class MainActivity extends Activity { private Button mButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); init(); } public void init() { mButton = (Button) findViewById(R.id.button); mButton.setOnClickListener(new ClickListenerImpl()); } private class ClickListenerImpl implements OnClickListener { @Override public void onClick(View v) { switch (v.getId()) { case R.id.button: showDialog(); break; default: break; } } } private void showDialog(){ LayoutInflater inflater=getLayoutInflater(); View dialogView=inflater.inflate(R.layout.test_dialog, null); Dialog dialog=new Dialog(MainActivity.this,R.style.dialog); dialog.setContentView(dialogView); dialog.getWindow().setLayout(440, LayoutParams.WRAP_CONTENT); dialog.show(); } }
main.xml如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:text="show dialog" /> </RelativeLayout>
test_dialog.xml如下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="match_parent" android:background="@android:color/white" android:orientation="vertical" > <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="username" /> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="password" /> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="login" /> </LinearLayout>
styles.xml如下:
<resources> <style name="AppBaseTheme" parent="android:Theme.Light"></style> <style name="AppTheme" parent="AppBaseTheme"></style> <style name="dialog" parent="@android:style/Theme.Dialog"> <item name="android:windowFrame">@null</item> <item name="android:windowIsFloating">true</item> <item name="android:windowIsTranslucent">true</item> <item name="android:windowNoTitle">true</item> <item name="android:windowBackground">@android:color/transparent</item> <item name="android:backgroundDimEnabled">true</item> <item name="android:windowContentOverlay">@null</item> </style> </resources>
时间: 2024-11-10 10:47:43