Android实现iphone样式的对话框,主要是借助shape,corner元素方法。
下面的circular_corner_dialog.xml文件定义了一个圆角矩形。corner元素指定了圆角矩形的圆角半径,而gradient元素则指定了色彩渐变的方向以及起始颜色。当然也可以使用shape创建其它各种图形,如椭圆、线条以及环等,并设置不同的视觉风格。
res/drawable/circular_corner_dialog.xml
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item> <shape> <gradient android:startColor="@android:color/white" android:endColor="@android:color/white"> </gradient> <corners android:topLeftRadius="15dp" android:topRightRadius="15dp" android:bottomLeftRadius="15dp" android:bottomRightRadius="15dp"> </corners> </shape> </item> </selector>
fragment_dialog_circular_corner.xml文件时自定义对话框MyDialogFragment的界面布局文件,创建MyDialogFragment实例时加载该界面布局。
res/layout/fragment_dialog_circular_corner.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/transparent" android:gravity="center" > <LinearLayout android:layout_width="100dp" android:layout_height="100dp" android:orientation="horizontal" android:background="@drawable/circular_corner_dialog" android:layout_gravity="center"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" /> </LinearLayout> </LinearLayout>
主Activity类MyActivity的fragment视图容器布局文件。
res/layout/activity_myactivity.xml
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:id="@+id/fragment_container" android:layout_width="match_parent" android:layout_height="match_parent"></FrameLayout>
自定义圆角对话框。
src/.../MyDialogFragment.java
import android.graphics.Color; import android.graphics.drawable.ColorDrawable; import android.support.v4.app.DialogFragment; import android.os.Bundle; import android.support.annotation.Nullable; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.Window; public class MyDialogFragment extends DialogFragment { View view; @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { super.onCreateView(inflater, container, savedInstanceState); view=inflater.inflate(R.layout.fragment_dialog_circular_corner,container,false); getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE); getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); return view; } }
托管Fragment的主Activity类。
src/.../MyActivity.java
import android.os.Bundle; import android.support.v4.app.FragmentActivity; import com.kanebay.dcide.R; public class MyActivity extends FragmentActivity { private MyDialogFragment myDialogFragment; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login_notice); if(savedInstanceState==null){ // getSupportFragmentManager() .beginTransaction() .add(R.id.fragment_container, myDialogFragment) .commit(); }else { myDialogFragment=(MyDialogFragment)getSupportFragmentManager() .findFragmentById(R.id.fragment_container); } } }