带有Button和ImageView的对话框?

问题描述:

我想用ImageView和一个按钮创建一个对话框。我添加和运行的ImageView,但是我无法在对话框中添加的按钮,该按钮不显示。 我该怎么办?带有Button和ImageView的对话框?

这里我怎么试。

XML

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"   
    > 

    <ImageView 
     android:id="@+id/ivCustomDialog" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:padding="0dp" 
     android:scaleType="centerCrop"     
     /> 

    <Button 
     android:id="@+id/btnProsseguir" 
     android:layout_width="100px" 
     android:layout_height="wrap_content" 
     android:background="@drawable/resp_prosseguir" 
     android:layout_marginTop="5dp" 
     android:layout_marginRight="5dp" 
     android:layout_below="@+id/ivCustomDialog" 
     /> 

</RelativeLayout> 

//活动

private void showDialog(final RespostaPergunta resposta){     
      final Dialog dialog = new Dialog(getView().getContext()); 
      dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); 
      dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT)); 
      dialog.getWindow().setFlags(LayoutParams.FLAG_FULLSCREEN, LayoutParams.FLAG_FULLSCREEN);       
      dialog.setContentView(R.layout.custom_dialog); 
      dialog.getWindow().setLayout(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT); 
      ImageView ivCustomDialog = (ImageView) dialog.findViewById(R.id.ivCustomDialog); 

      Button dialogButton = (Button) dialog.findViewById(R.id.btnProsseguir); 
      dialogButton.setOnClickListener(new OnClickListener() {    
       @Override 
       public void onClick(View v) { 
        FragmentTransaction ft; 
        Fragment frag = new InicioFrag(); 
        if(resposta.getValor() == 1){ 
         frag = new SalaInternaFrag(); 
         ft = getFragmentManager().beginTransaction(); 
         ft.replace(R.id.fl, frag); 
         ft.addToBackStack("back"); 
         ft.commit(); 
        }else{ 
         frag = new InicioFrag(); 
         ft = getFragmentManager().beginTransaction(); 
         ft.replace(R.id.fl, frag); 
         ft.addToBackStack("back"); 
         ft.commit();       
        }      
        dialog.dismiss();     
       } 
      });   

      if(resposta.getValor() == 1){ 
       ivCustomDialog.setImageResource(R.drawable.resp_correto); 
      }else{ 
       ivCustomDialog.setImageResource(R.drawable.resp_errado); 
      } 

      dialog.show(); 

     } 
+0

将'RelativeLayout'的'height'改为'wrap_content' – 2014-12-03 20:34:46

+0

我改变了,但不行! – FernandoPaiva 2014-12-03 20:41:51

+0

该按钮没有显示?或按钮点击不起作用? – Rami 2014-12-03 20:52:46

我解决了这个问题。

我做到了。

<RelativeLayout  
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"   
    xmlns:android="http://schemas.android.com/apk/res/android"> 


     <ImageView 
      android:id="@+id/ivCustomDialog" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:padding="0dp" 
      android:scaleType="centerCrop"     
      /> 


     <Button 
      android:id="@+id/btnProsseguir" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:background="@drawable/resp_prosseguir"      
      android:layout_alignParentBottom="true" 
      android:layout_centerHorizontal="true"   
      /> 


</RelativeLayout> 

也没准按钮添加到对话,但可以看到它,因为所有的空间由图像占用,尝试将图像视图和按钮添加到滚动视图(垂直) 类似这样的东西

<ScrollView 
.. 
.. > 
    <LinearLayout ...> 
     <ImageView .../> 
     <Button .... /> 
    </LinearLayout> 
</ScrollView> 

运行,如果你能看到按钮向下滚动以及你只需要改变图像的宽度和高度