0

Androidアプリケーション用のカスタムダイアログがあります。画像とテキストが表示され、左右に2つのボタンがあります。これが私のダイアログです

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>

このダイアログを親の中心に正確に表示するにはどうすればよいですか?

4

1 に答える 1

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>
于 2012-09-21T10:39:58.557 に答える