为什么我在自定义android对话框中对齐按钮时遇到问题?

时间:2011-07-26 15:36:34

标签: android layout button dialog

当我在模拟器中运行对话框时,按钮全部间隔开,右边的按钮甚至都没有出现,我希望它们在中心紧紧地粘在一起。

这是我的代码:

custom_dialog.xml

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

       <ImageView 
         android:id="@+id/ImageView01"
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content"
         android:layout_centerHorizontal="true" />

       <ScrollView 
         android:id="@+id/ScrollView01"
         android:layout_width="wrap_content" 
         android:layout_below="@+id/ImageView01"
         android:layout_height="200px">

         <TextView 
           android:text="@+id/TextView01" 
           android:id="@+id/TextView01"
           android:layout_width="wrap_content" 
           android:layout_height="wrap_content" />
       </ScrollView>

       <RelativeLayout android:background="#f9d407" 
         android:id="@+id/RelativeLayout01"  android:layout_width="fill_parent"
         android:layout_height="wrap_content" android:layout_below="@id/ScrollView01">

         <Button android:id="@+id/Button01"  android:text="Top"  android:layout_height="30dp"   android:textSize="12sp"  android:layout_width="wrap_content"   android:layout_alignParentTop="true"   android:layout_centerInParent="true"></Button>
         <Button android:id="@+id/button3"  android:text="Proven"  android:layout_width="wrap_content"  android:layout_height="30dp"  android:textSize="12sp" android:layout_alignParentTop="true"  android:layout_toLeftOf="@+id/button1"></Button>
         <Button android:id="@+id/button2"  android:text="Recent"  android:layout_height="30dp"  android:textSize="12sp" android:layout_width="wrap_content"  android:layout_toRightOf="@+id/button1" ></Button>
       </RelativeLayout>

     </RelativeLayout>

##的 CustomDialog.java ##

  

package com.testing.CustomDialog;

     

import android.app.Activity; import android.app.Dialog;进口   android.os.Bundle; import android.view.View;进口   android.view.View.OnClickListener; import android.widget.Button;   import android.widget.ImageView; import android.widget.TextView;

  public class CustomDialog extends Activity {
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          //set up main content view
          setContentView(R.layout.main);
          //this button will show the dialog
          Button button1main = (Button)
     

findViewById(R.id.Button01main);                 button1main.setOnClickListener(new OnClickListener(){

          @Override
              public void onClick(View v) {
                  //set up dialog
                  Dialog dialog = new Dialog(CustomDialog.this);
                  dialog.setContentView(R.layout.custom_dialog);
                  dialog.setTitle("This is my custom dialog box");
                  dialog.setCancelable(true);
                  //there are a lot of settings, for dialog, check
     他们全都出来了!

                  //set up text
                  TextView text = (TextView)
     

dialog.findViewById(R.id.TextView01);                         text.setText(R.string.lots_of_text);

                  //set up image view
                  ImageView img = (ImageView)
     

dialog.findViewById(R.id.ImageView01);                         img.setImageResource(R.drawable.icon);

                  //set up button
                  Button button = (Button)
     

dialog.findViewById(R.id.Button01);                         button.setOnClickListener(new OnClickListener()   {                         @覆盖                             public void onClick(查看v){                                 完();                         }

                  });
                  Button button2 = (Button)
     

dialog.findViewById(R.id.button3);

                  //now that the dialog is set up, it's time to
     

显示它                         dialog.show();                     }                 });             }          }

1 个答案:

答案 0 :(得分:0)

我相信你也可能有拼写错误,看看你尝试使用Button1引用它的Button01 这应该是它的样子。希望这可以帮助 :) `      

   <ImageView 
     android:id="@+id/ImageView01"
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"
     android:layout_centerHorizontal="true" />

   <ScrollView 
     android:id="@+id/ScrollView01"
     android:layout_width="wrap_content" 
     android:layout_below="@+id/ImageView01"
     android:layout_height="200px">

     <TextView 
       android:text="@+id/TextView01" 
       android:id="@+id/TextView01"
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" />
   </ScrollView>

   <RelativeLayout android:background="#f9d407" 
     android:id="@+id/RelativeLayout01"  android:layout_width="fill_parent"
     android:layout_height="wrap_content" android:layout_below="@id/ScrollView01">
     <Button android:text="Recent" android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="30dp" android:textSize="12sp" android:layout_alignParentTop="true" android:layout_toLeftOf="@+id/Button01"></Button>
     <Button android:text="Proven" android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="30dp" android:textSize="12sp" android:layout_alignParentTop="true" android:layout_toRightOf="@+id/Button01"></Button>
     <Button android:text="Top" android:id="@+id/Button01" android:layout_width="wrap_content" android:layout_height="30dp" android:textSize="12sp" android:layout_alignParentTop="true" android:layout_centerHorizontal="true"></Button>
   </RelativeLayout>

 </RelativeLayout>`