ImageButton +对话框问题

时间:2013-05-11 21:15:11

标签: java android xml layout

嘿我正在尝试进行对话。但整个对话框没有显示,只有底部的3个图像按钮显示:这里有什么问题?当我点击对话框内的按钮时,即使我做了一个开关盒,也没有任何反应。

customtype_dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".AutoMode"
    android:background="@color/black" >    

<RelativeLayout 
    android:id="@+id/dialog_relativeLayout1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="3dp"    
    android:layout_above="@+id/dialog_relativeLayout2"
    android:layout_centerHorizontal="true" >

    <Button
        android:id="@+id/dialog_btn1"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:layout_margin="3dp"
        android:background="@drawable/track_run"
        android:layout_alignRight="@+id/dialog_relativeLayout1"
        android:layout_alignTop="@+id/dialog_relativeLayout1"
        />

    <Button 
        android:id="@+id/dialog_btn2"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:layout_margin="3dp"
        android:background="@drawable/track_bike"
        android:layout_alignTop="@+id/dialog_relativeLayout1"
        android:layout_toRightOf="@+id/dialog_btn1"
        />

    <Button 
        android:id="@+id/dialog_btn3"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:layout_margin="3dp"
        android:layout_toRightOf="@+id/dialog_btn2"
        android:background="@drawable/track_walk"
        />
 </RelativeLayout>

<RelativeLayout 
    android:id="@+id/dialog_relativeLayout2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="3dp"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    >

    <Button 
        android:id="@+id/dialog_btn4"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:layout_margin="3dp"
        android:background="@drawable/track_ski"
        android:layout_alignRight="@+id/dialog_relativeLayout2"
        android:layout_alignTop="@+id/dialog_relativeLayout2"
        />

    <Button 
        android:id="@+id/dialog_btn5"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:layout_margin="3dp"
        android:background="@drawable/track_boat"
        android:layout_alignTop="@+id/dialog_relativeLayout2"
        android:layout_toRightOf="@+id/dialog_btn4"
        />

    <Button 
        android:id="@+id/dialog_btn6"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:layout_margin="3dp"
        android:background="@drawable/track_snow_boarding"
        android:layout_alignTop="@+id/dialog_relativeLayout2"
        android:layout_toRightOf="@+id/dialog_btn5"
        />
    </RelativeLayout>
</RelativeLayout>




routeType = (ImageButton)findViewById(R.id.saveRoute_activityType);
        routeType.setOnClickListener(typeListener);

SaveRouteActivity.java

这是我的倾听者。

View.OnClickListener typeListener = new OnClickListener() {
                public void onClick(View v) {

                    switch(v.getId()){
                    case R.id.saveRoute_activityType:
                        Dialog typedialog = new Dialog(SaveRouteActivity.this);
                        typedialog.setContentView(R.layout.customtype_dialog);
                        typedialog.setTitle("Select type");
                        typedialog.setCancelable(true);
                        typedialog.show();
                        break;

                    case R.id.dialog_btn1:
                        txtType.setText("Run");
                        routeType.setBackgroundResource(R.drawable.track_run);
                        break;

                    case R.id.dialog_btn2:
                        txtType.setText("Bike");
                        routeType.setBackgroundResource(R.drawable.track_bike);
                        break;

                    case R.id.dialog_btn3:
                        txtType.setText("Walk");
                        routeType.setBackgroundResource(R.drawable.track_walk);
                        break;

                    case R.id.dialog_btn4:
                        txtType.setText("Ski");
                        routeType.setBackgroundResource(R.drawable.track_ski);
                        break;

                    case R.id.dialog_btn5:
                        txtType.setText("Boat");
                        routeType.setBackgroundResource(R.drawable.track_boat);
                        break;

                    case R.id.dialog_btn6:
                        txtType.setText("Snowboarding");
                        routeType.setBackgroundResource(R.drawable.track_snow_boarding);
                        break;
                    }

                }
            };

1 个答案:

答案 0 :(得分:0)

我的猜测是typedialog.setContentView(R.layout.customtype_dialog);被忽略,因为Dialog尚未膨胀。要解决此问题,您应该创建Dialog的子类,并在其setContentView()方法中调用onCreate()