创建自定义AlertDialog框的高度和宽度

时间:2017-11-23 07:17:01

标签: android alertdialog

我尝试使用自定义alertdialog框的滚动视图创建自定义高度和重量,但它对我不起作用。任何人都可以解释我如何使用scrollview创建自定义alertdialog框自定义重量和高度?我的xml文件上是否有错误的代码?

.XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="268dp"
    android:id="@+id/zonevi_clubs"
    android:layout_margin="30dp">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="268dp"
        android:fillViewport="true">
        <ImageView
            android:id="@+id/club1"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:layout_marginStart="32dp"
            android:layout_marginTop="25dp"
            android:clickable="true"
            android:src="@mipmap/ic_rtr" />
        <ImageView
            android:id="@+id/club2"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_alignParentEnd="true"
            android:layout_alignTop="@+id/club1"
            android:layout_marginEnd="32dp"
            android:clickable="true"
            android:src="@mipmap/ic_rtr" />

        <ImageView
            android:id="@+id/club3"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_alignStart="@+id/club1"
            android:layout_below="@+id/club1"
            android:layout_marginTop="21dp"
            android:clickable="true"
            android:src="@mipmap/ic_rtr" />
        <ImageView
            android:id="@+id/club4"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_alignStart="@+id/club2"
            android:layout_alignTop="@+id/club3"
            android:clickable="true"
            android:src="@mipmap/ic_rtr" />

        <ImageView
            android:id="@+id/club5"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_alignStart="@+id/club1"
            android:layout_below="@+id/club3"
            android:layout_marginTop="21dp"
            android:clickable="true"
            android:src="@mipmap/ic_rtr" />
        <ImageView
            android:id="@+id/club6"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_alignStart="@+id/club4"
            android:layout_alignTop="@+id/club5"
            android:clickable="true"
            android:src="@mipmap/ic_rtr" />

        <ImageView
            android:id="@+id/club7"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_alignStart="@+id/club5"
            android:layout_below="@+id/club5"
            android:layout_marginTop="27dp"
            android:clickable="true"
            android:src="@mipmap/ic_rtr" />
        <ImageView
            android:id="@+id/club8"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_alignStart="@+id/club6"
            android:layout_alignTop="@+id/club7"
            android:clickable="true"
            android:src="@mipmap/ic_rtr" />
    </ScrollView>
</RelativeLayout>

我的自定义alertdialog如下:

AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                    View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.zonevi_clubs, null);

                    builder.setPositiveButton("Thank You", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            Toast.makeText(MainActivity.this, "It's My Pleasure", Toast.LENGTH_SHORT).show();
                        }
                    });

                    builder.setView(view);
                    builder.show();

2 个答案:

答案 0 :(得分:1)

  

ScrollView 只有一个孩子

ImageView 中的所有 LinearLayout 包裹在下面的布局中

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/zonevi_clubs"
    android:layout_width="match_parent"
    android:layout_height="268dp"
    android:layout_margin="30dp">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fillViewport="true">

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

            <ImageView
                android:id="@+id/club1"
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:layout_alignParentStart="true"
                android:layout_alignParentTop="true"
                android:layout_marginStart="32dp"
                android:layout_marginTop="25dp"
                android:clickable="true"
                android:src="@mipmap/ic_rtr" />

            <ImageView
                android:id="@+id/club2"
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:layout_alignParentEnd="true"
                android:layout_alignTop="@+id/club1"
                android:layout_marginEnd="32dp"
                android:clickable="true"
                android:src="@mipmap/ic_rtr" />

            <ImageView
                android:id="@+id/club3"
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:layout_alignStart="@+id/club1"
                android:layout_below="@+id/club1"
                android:layout_marginTop="21dp"
                android:clickable="true"
                android:src="@mipmap/ic_rtr" />

            <ImageView
                android:id="@+id/club4"
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:layout_alignStart="@+id/club2"
                android:layout_alignTop="@+id/club3"
                android:clickable="true"
                android:src="@mipmap/ic_rtr" />

            <ImageView
                android:id="@+id/club5"
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:layout_alignStart="@+id/club1"
                android:layout_below="@+id/club3"
                android:layout_marginTop="21dp"
                android:clickable="true"
                android:src="@mipmap/ic_rtr" />

            <ImageView
                android:id="@+id/club6"
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:layout_alignStart="@+id/club4"
                android:layout_alignTop="@+id/club5"
                android:clickable="true"
                android:src="@mipmap/ic_rtr" />

            <ImageView
                android:id="@+id/club7"
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:layout_alignStart="@+id/club5"
                android:layout_below="@+id/club5"
                android:layout_marginTop="27dp"
                android:clickable="true"
                android:src="@mipmap/ic_rtr" />

            <ImageView
                android:id="@+id/club8"
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:layout_alignStart="@+id/club6"
                android:layout_alignTop="@+id/club7"
                android:clickable="true"
                android:src="@mipmap/ic_rtr" />

        </LinearLayout>
    </ScrollView>
</RelativeLayout>

java代码

AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.zonevi_clubs, null);
builder.setView(view);
AlertDialog dialog_card = build.create();
Window window = dialog_card.getWindow();
window.setLayout(WindowManager.LayoutParams.MATCH_PARENT,268);
window.setGravity(Gravity.CENTER);
build.show();

答案 1 :(得分:0)

试试这个。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="268dp"
android:id="@+id/zonevi_clubs"
android:layout_margin="30dp">

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="268dp"
    android:fillViewport="true">

  <RelativeLayout 
    android:layout_width="match_parent"
    android:layout_height="400dp" /* 4(100dp) * 2 height of imageview. */ 
    android:id="@+id/zonevi_clubs_scroll_root"
    >
    <ImageView
        android:id="@+id/club1"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginStart="32dp"
        android:layout_marginTop="25dp"
        android:clickable="true"
        android:src="@mipmap/ic_rtr" />
    <ImageView
        android:id="@+id/club2"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_alignParentEnd="true"
        android:layout_alignTop="@+id/club1"
        android:layout_marginEnd="32dp"
        android:clickable="true"
        android:src="@mipmap/ic_rtr" />

    <ImageView
        android:id="@+id/club3"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_alignStart="@+id/club1"
        android:layout_below="@+id/club1"
        android:layout_marginTop="21dp"
        android:clickable="true"
        android:src="@mipmap/ic_rtr" />
    <ImageView
        android:id="@+id/club4"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_alignStart="@+id/club2"
        android:layout_alignTop="@+id/club3"
        android:clickable="true"
        android:src="@mipmap/ic_rtr" />

    <ImageView
        android:id="@+id/club5"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_alignStart="@+id/club1"
        android:layout_below="@+id/club3"
        android:layout_marginTop="21dp"
        android:clickable="true"
        android:src="@mipmap/ic_rtr" />
    <ImageView
        android:id="@+id/club6"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_alignStart="@+id/club4"
        android:layout_alignTop="@+id/club5"
        android:clickable="true"
        android:src="@mipmap/ic_rtr" />

    <ImageView
        android:id="@+id/club7"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_alignStart="@+id/club5"
        android:layout_below="@+id/club5"
        android:layout_marginTop="27dp"
        android:clickable="true"
        android:src="@mipmap/ic_rtr" />
    <ImageView
        android:id="@+id/club8"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_alignStart="@+id/club6"
        android:layout_alignTop="@+id/club7"
        android:clickable="true"
        android:src="@mipmap/ic_rtr" />
</ScrollView>