2012-09-21 24 views
0

我有我的Android application.it自定义对话框中显示的图像和文本与左边两个按钮和right.Here是我的对话框如何显示自定义对话框,在中心

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> 

我该如何显示这个对话正好在父母的中心。

+0

可能是主题的问题..尝试使用不同的.. – Ronnie

+0

现在怎么出现的? – sujith

+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> 
相关问题