如何在中心显示自定义对话框

时间:2012-09-21 09:44:07

标签: android customdialog

我的android应用程序有一个自定义对话框。它显示了左右两个按钮的图像和文本。这是我的对话框

public class GalleryPopUp extends Dialog {

    private Context context;
    private ArrayList<Bitmap> bitmapList = null;
    private List<News> homeNewsList = null;
    private Button btnArrowLeft;
    private Button btnArrowRight;
    private ImageView imageView;
    private int selectedIndex = 0;
    private TextView textView;
    static final RelativeLayout.LayoutParams FILL = new RelativeLayout.LayoutParams(
            LayoutParams.FILL_PARENT,
            LayoutParams.FILL_PARENT);

    public GalleryPopUp(Context context, ArrayList<Bitmap> bitmapList,
            List<News> homeNewsList, int selectedIndex) {
        super(context, android.R.style.Theme_Translucent_NoTitleBar);
        this.context = context;
        this.bitmapList = bitmapList;
        this.homeNewsList = homeNewsList;
        this.selectedIndex = selectedIndex;

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.gallery_popup, null);
        btnArrowLeft = (Button)view.findViewById(R.id.btnArrowLeft);
        btnArrowRight = (Button)view. findViewById(R.id.btnArrowRight);
        imageView = (ImageView) view.findViewById(R.id.imageView);
        textView = (TextView)view. findViewById(R.id.textView);
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
        imageView.setImageBitmap(bitmapList.get(selectedIndex));
        News news = homeNewsList.get(selectedIndex);
        textView.setTextSize(15);
        textView.setTypeface(TypeFaceUtils.TYPEFACE_THOOLIKA);
        textView.setText(news.getNewsTitle());
        btnArrowLeft.setOnClickListener(listenerbtnArrowLeft);
        btnArrowRight.setOnClickListener(listenerbtnArrowRight);
        setContentView(view);
    }

    private android.view.View.OnClickListener listenerbtnArrowLeft = new android.view.View.OnClickListener() {

        @Override
        public void onClick(View v) {
            selectedIndex = selectedIndex - 1;
            if (selectedIndex < homeNewsList.size() && selectedIndex >= 0) {
                imageView.setImageBitmap(bitmapList.get(selectedIndex));
                News news = homeNewsList.get(selectedIndex);
                textView.setTypeface(TypeFaceUtils.TYPEFACE_THOOLIKA);
                textView.setText(news.getNewsTitle());
            }
            else
            {
                GalleryPopUp.this.dismiss();
            }
        }
    };

    private android.view.View.OnClickListener listenerbtnArrowRight = new android.view.View.OnClickListener() {

        @Override
        public void onClick(View v) {
            selectedIndex = selectedIndex + 1;
            if (selectedIndex < homeNewsList.size()) {
                imageView.setImageBitmap(bitmapList.get(selectedIndex));
                News news = homeNewsList.get(selectedIndex);
                textView.setTypeface(TypeFaceUtils.TYPEFACE_THOOLIKA);
                textView.setText(news.getNewsTitle());
            }
            else
            {
                GalleryPopUp.this.dismiss();
            }
        }
    };
}

gallery_popup.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relativeLayout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/imageView"
        android:layout_gravity="center_horizontal|bottom"
        android:gravity="center_horizontal"
        android:text=""
        android:textColor="#000000" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/imageView"
        android:layout_alignParentRight="true"
        android:layout_alignTop="@+id/imageView"
        android:gravity="center" >

        <Button
            android:id="@+id/btnArrowRight"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/btn_arrow_right" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/imageView"
        android:layout_alignParentLeft="true"
        android:layout_alignTop="@+id/imageView"
        android:gravity="center" >

        <Button
            android:id="@+id/btnArrowLeft"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:background="@drawable/btn_arrow_left" />
    </LinearLayout>

</RelativeLayout>

如何在父母的中心显示此对话框?

1 个答案:

答案 0 :(得分:0)

最好的方法是改变你的布局:

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    <RelativeLayout 
        android:id="@+id/relativeLayout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"

    android:layout_centerInParent="true"
 >

尝试一下,让我知道它是否有效。             

        <TextView
            android:id="@+id/textView"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/imageView"
            android:layout_gravity="center_horizontal|bottom"
            android:gravity="center_horizontal"
            android:text=""
            android:textColor="#000000" />

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/imageView"
            android:layout_alignParentRight="true"
            android:layout_alignTop="@+id/imageView"
            android:gravity="center" >

            <Button
                android:id="@+id/btnArrowRight"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/btn_arrow_right" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/imageView"
            android:layout_alignParentLeft="true"
            android:layout_alignTop="@+id/imageView"
            android:gravity="center" >

            <Button
                android:id="@+id/btnArrowLeft"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_centerVertical="true"
                android:background="@drawable/btn_arrow_left" />
        </LinearLayout>

    </RelativeLayout>
   </RelativeLayout>
相关问题