弹出对话框中没有在cardview中显示OK按钮

时间:2017-04-28 06:11:01

标签: java android xml android-layout android-bluetooth

我是Android应用开发的新手。我正在android-studio中创建一个项目。我想显示弹出对话框,无论我在xml文件中声明了什么,但是OK按钮没有显示。如果有人知道,请分享你的answer.xml和java代码

image_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical">
<!--<ImageView android:layout_width="wrap_content"-->
    <!--android:layout_height="wrap_content" android:src="YOUR IMAGE"/>-->

<android.support.v7.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/cardViewbluetooth"
    app:cardCornerRadius="5dp"
    android:layout_gravity="center"
    android:layout_marginLeft="20dp"
    android:layout_marginRight="20dp"
    android:paddingTop="180dp"
    >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Bluetooth not enabled"
            android:textSize="20dp"
            android:textStyle="bold"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Please enable bluetooth in settings and restart this application"
            android:textStyle="bold"/>
        <Button android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:text="OK" android:onClick="dismissListener"/>

    </LinearLayout>
</android.support.v7.widget.CardView></LinearLayout>

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    tools:context="litifer.awesome.game.litifer_carddemo.MainActivity"
    >

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/cardView1"
        app:cardCornerRadius="5dp"
        android:layout_gravity="center"
        android:layout_marginTop="10dp"
        app:cardElevation="0dp"
        android:clickable="true"
        app:cardBackgroundColor="@android:color/transparent"
        android:background="#33FF0000"
        >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/imageBeacon"
                android:src="@drawable/beacon"
                android:adjustViewBounds="true"
                android:layout_gravity="center"
                />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/cardView1text"
                android:text="Beacon Demo"
                android:layout_marginLeft="20dp"
                android:layout_marginTop="10dp"
                android:textSize="20dp"
                android:textStyle="bold"
                android:layout_marginRight="20dp"
                android:layout_marginBottom="10dp"
                />

        </LinearLayout>
    </android.support.v7.widget.CardView>

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/cardView2"
        app:cardCornerRadius="5dp"
        android:layout_gravity="center"
        android:layout_marginTop="10dp"
        app:cardElevation="0dp"
        android:clickable="true"
        app:cardBackgroundColor="@android:color/transparent"
        android:background="#33FF0000"
        >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/imageWifi"
                android:src="@drawable/wifi"
                android:adjustViewBounds="true"
                android:layout_gravity="center"
                />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/cardView2text"
                android:text="Wifi Demo"
                android:layout_marginLeft="20dp"
                android:layout_marginTop="10dp"
                android:textSize="20dp"
                android:textStyle="bold"
                android:layout_marginRight="20dp"
                android:layout_marginBottom="10dp"
                />
        </LinearLayout>
    </android.support.v7.widget.CardView>

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/cardView3"
        app:cardCornerRadius="5dp"
        android:layout_gravity="center"
        android:layout_marginTop="10dp"
        app:cardElevation="0dp"
        android:clickable="true"
        app:cardBackgroundColor="@android:color/transparent"
        android:background="#33FF0000"
        >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/imageCustomerReview"
                android:src="@drawable/customerreview"
                android:adjustViewBounds="true"
                android:layout_gravity="center"
                />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/cardView3text"
                android:text="CustomerReview Demo"
                android:layout_marginLeft="20dp"
                android:layout_marginTop="10dp"
                android:textSize="20dp"
                android:textStyle="bold"
                android:layout_marginRight="20dp"
                android:layout_marginBottom="10dp"
                />
        </LinearLayout>
    </android.support.v7.widget.CardView>

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/cardView4"
        app:cardCornerRadius="5dp"
        android:layout_gravity="center"
        android:layout_marginTop="10dp"
        app:cardElevation="0dp"
        android:clickable="true"
        app:cardBackgroundColor="@android:color/transparent"
        android:background="#33FF0000"
        >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/imagecardview"
                android:src="@drawable/cardview"
                android:adjustViewBounds="true"
                android:layout_gravity="center"
                />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/cardView4text"
                android:text="CardView Demo"
                android:layout_marginLeft="20dp"
                android:layout_marginTop="10dp"
                android:textSize="20dp"
                android:textStyle="bold"
                android:layout_marginRight="20dp"
                android:layout_marginBottom="10dp"
                />
        </LinearLayout>
    </android.support.v7.widget.CardView>

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/cardView5"
        app:cardCornerRadius="5dp"
        android:layout_gravity="center"
        android:layout_marginTop="10dp"
        app:cardElevation="0dp"
        android:clickable="true"
        app:cardBackgroundColor="@android:color/transparent"
        android:background="#33FF0000"
        >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/imagedemo"
                android:src="@drawable/demo"
                android:adjustViewBounds="true"
                android:layout_gravity="center"
                />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/cardView5text"
                android:text="Test All Demo"
                android:layout_marginLeft="20dp"
                android:layout_marginTop="10dp"
                android:textSize="20dp"
                android:textStyle="bold"
                android:layout_marginRight="20dp"
                android:layout_marginBottom="10dp"
                />
        </LinearLayout>
    </android.support.v7.widget.CardView>
</LinearLayout> </ScrollView>

MainActivity.java

public class MainActivity extends AppCompatActivity {

    private ImageView beacon,cardview,customerreview,demo,wifi;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        beacon = (ImageView)findViewById(R.id.imageBeacon);
        beacon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
                if (mBluetoothAdapter == null) {
                    // Device does not support Bluetooth
                } else {
                    if (!mBluetoothAdapter.isEnabled()) {
                        // Bluetooth is not enable :)

                        Dialog settingsDialog = new Dialog(MainActivity.this);
                        settingsDialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
                        settingsDialog.setContentView(getLayoutInflater().inflate(R.layout.image_layout
                                , null));
                        settingsDialog.show();
                    }
                }

            }
        });
    }
}

1 个答案:

答案 0 :(得分:0)

试试这个

Dialog settingsDialog = new Dialog(this);
        settingsDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        settingsDialog.setContentView(R.layout.image_layout);
        settingsDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        settingsDialog.setCancelable(true);
        settingsDialog.setTitle("Title...");
        settingsDialog.show();