【问题标题】:Custom Image Dialog layout自定义图像对话框布局
【发布时间】:2017-02-07 08:37:50
【问题描述】:

要求

我想在自定义对话框中显示带有边框和关闭按钮的图像,ImageViewTouchImageView 用于放大/缩小功能。像这样

https://i.stack.imgur.com/nN1jJ.jpg

我试过了

我尝试了很多方法来设置边框,但没有得到解决方案。

我的代码

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

    <com.android.example.view.TouchImageView
        android:id="@+id/imageSingleImage"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@mipmap/ic_launcher" />
</RelativeLayout>

使用@user2025187 代码我在输出中得到了这个

https://i.stack.imgur.com/Cv6Ph.png

我正在点击这张图片

https://i.stack.imgur.com/LA8Uc.png

【问题讨论】:

  • 已经对您的图像添加了边框效果。有什么问题?
  • 我想要那种类型的对话

标签: android android-layout android-imageview


【解决方案1】:

这是一个简单的东西,你也可以在谷歌上搜索它。

在drawable文件夹中创建一个布局文件并将下面的代码添加为rectangle.xml

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#FFFFFF" />
<stroke android:width="1dp" android:color="#000000" />
<padding android:left="1dp" android:top="1dp" android:right="1dp"
android:bottom="1dp" />

创建一个布局文件并添加这个 xml。前-dialog.xml

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

<com.android.example.view.TouchImageView
    android:id="@+id/imageDialog"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginRight="6dp" 
    android:background="@drawable/rectangle"/>

<TextView
    android:id="@+id/textDialog"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textColor="#FFF"
    android:layout_toRightOf="@+id/imageDialog"/>

 <Button
    android:id="@+id/declineButton"
    android:layout_width="100px"
    android:layout_height="wrap_content"
    android:text=" Submit "
    android:layout_marginTop="5dp"
    android:layout_marginRight="5dp"
    android:layout_below="@+id/textDialog"
    android:layout_toRightOf="@+id/imageDialog"
    />

  </RelativeLayout>

在你的 java 文件中添加下面的代码,你的对话框就准备好了。

          final Dialog dialog = new Dialog(CustomDialog.this);
            // Include dialog.xml file
            dialog.setContentView(R.layout.dialog);
            // Set dialog title
            dialog.setTitle("Custom Dialog");

            // set values for custom dialog components - text, image and button
            TextView text = (TextView) dialog.findViewById(R.id.textDialog);
            text.setText("Custom dialog Android example.");
            ImageView image = (ImageView) dialog.findViewById(R.id.imageDialog);
            image.setImageResource(R.drawable.image0);

            dialog.show();

            Button declineButton = (Button) dialog.findViewById(R.id.declineButton);
            // if decline button is clicked, close the custom dialog
            declineButton.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    // Close dialog
                    dialog.dismiss();
                }
            });

【讨论】:

  • 我不想知道如何制作自定义对话框,我很清楚。但我的问题是我想要 imageview 之外的边框,它应该是灵活的。
  • 边框不是用于imageview,而是用于dailog
  • @PatelPinkal 您希望在图像视图或对话框上设置边框
  • android:background="@drawable/rectangle" 将此添加到您的主布局或在运行时在对话框中添加此背景,如果您想在对话框上添加边框
  • 您可以在 imageview 或对话框上应用边框,但我的 imageView 是自定义缩放的。每当我尝试为图像视图设置边框时,它都会设置修复图像具有的高度和宽度,但我想要全屏显示。并且每当我将边框应用于对话框时,它都会包含额外的空间,因为某些图像高度很大或宽度很大
【解决方案2】:

试试这个,

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:layout_centerHorizontal="true"
      android:layout_centerVertical="true"
      android:layout_margin="10dp">

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:layout_margin="10dp"
    android:background="@color/colorWhite">

    <com.android.example.view.TouchImageView
        android:id="@+id/iv_imageSingleImage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:contentDescription="@string/contentDescription"
        android:padding="5dp" />

</RelativeLayout>

<ImageView
    android:id="@+id/iv_close"
    android:layout_width="30dp"
    android:layout_height="30dp"
    android:layout_alignParentEnd="true"
    android:layout_alignParentTop="true"
    android:src="@drawable/ic_cross" />
</RelativeLayout>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多