как получить перекрестную кнопку/изображение в пользовательской границе диалогового окна

Я использовал диалоговое окно cutsom в моем MainActivity для всплывающего экрана. Я добавил перекрестную кнопку, чтобы закрыть всплывающий экран. здесь, в моем коде, эта крестообразная кнопка отображается внутри всплывающего экрана. Теперь я хочу, чтобы он отображался на границе всплывающего экрана. пожалуйста, помогите мне получить это ..

Ниже мой код... буду признателен за любую помощь... заранее спасибо...

Java-файл

Домашняя страница.java

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Point;
import android.os.Bundle;
import android.view.Display;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.support.v4.app.DialogFragment;

public class HomePage extends Activity {

final Context context = this;

Button b1;
Button b2;

public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home_page);

    b1 = (Button) findViewById(R.id.button6);
    b2 = (Button) findViewById(R.id.button7);
    b2.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent intent = new Intent(HomePage.this,RegisterPage.class);
            startActivity(intent);
        }

    });

    b1.setOnClickListener(new OnClickListener() {

      @SuppressLint("NewApi")
    @Override
      public void onClick(View arg0) {

        // custom dialog
    final Dialog dialog = new Dialog(context);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.setContentView(R.layout.activity_login_page);

        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); 
        Display display = wm.getDefaultDisplay(); // getting the screen size of device
        Point size = new Point();
        display.getSize(size);
        int width = size.x - 20;  // Set your heights
        int height = size.y - 80; // set your widths

        WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
        lp.copyFrom(dialog.getWindow().getAttributes());

        lp.width = width;
        lp.height = height;

        dialog.getWindow().setAttributes(lp);
        dialog.show();


        ImageView image = (ImageView) dialog.findViewById(R.id.cancel_btn);
        image.setImageResource(R.drawable.cancel2);
        image.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                dialog.dismiss();
            }

        });
    Button dialogButton = (Button) dialog.findViewById(R.id.button1);
    dialogButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
        //dialog.dismiss();
        Intent intent = new Intent(HomePage.this,CategoryPage.class);
        startActivity(intent);
            }
    });

        dialog.show();  
      }
    });

}
}

XML-файл

activity_home_page.xml

<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"
android:background="#fff"

tools:context=".MainActivity" >

<LinearLayout android:id="@+id/header"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#fff"
        android:paddingLeft="0dp"
        android:paddingRight="0dp"
        android:paddingTop="5dip"
        android:paddingBottom="5dip">

 <ImageView
    android:id="@+id/imageView1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_marginTop="0dp"
    android:src="@drawable/miiskylogo" />

 </LinearLayout>

 <ScrollView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_below="@+id/header" >

  <LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="163dp"
    android:orientation="vertical"
    android:paddingBottom="0dp"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin" >


   <Button
            android:id="@+id/button6"
            android:layout_width="230dp"
            android:layout_height="50dp"
            android:background="#00b0ff"
            android:layout_weight="0.1666"
            android:textSize="18dp"
            android:textColor="#fff"
            android:paddingLeft="3dp"
            android:layout_gravity="center"
            android:layout_marginTop="32dp"
            android:drawableLeft="@drawable/lock"
            android:text="Login with SVAPP" />

   <Button
            android:id="@+id/button7"
            android:layout_width="230dp"
            android:layout_height="50dp"
            android:background="#00b0ff"
            android:layout_weight="0.1666"
            android:textSize="18dp"
            android:textColor="#fff"
            android:paddingLeft="3dp"
            android:layout_gravity="center"
            android:layout_marginTop="15dp"
            android:drawableLeft="@drawable/regis"
            android:text="Register with SVAPP" />
 </LinearLayout>

</ScrollView>
</RelativeLayout>

activity_login_page.xml

<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"

android:background="#fff"
tools:context="com.example.miiskyproject.Login" >

<ImageView
android:id="@+id/cancel_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:src="@drawable/cancel2" />

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="130dp"
    android:layout_height="90dp"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="34dp"
    android:src="@drawable/miiskylogo" />

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

    <EditText
        android:id="@+id/editText1"
        android:layout_width="80dp"
        android:layout_height="35dp"
        android:layout_marginTop="190dp"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="10dp"
        android:background="@drawable/loginedit"
        android:ems="6"
        android:hint="User Name"
        android:padding="8dp"
        android:textColor="#000"
        android:textStyle="bold"
        android:textColorHint="#bdbdbd"
        android:textSize="13dp" >

        <requestFocus />
    </EditText>

    <EditText
        android:id="@+id/editText2"
        android:layout_width="80dp"
        android:layout_height="35dp"
        android:layout_marginTop="190dp"
        android:layout_marginRight="15dp"
        android:background="@drawable/loginedit"
        android:ems="6"
        android:hint="Password"
        android:inputType="textPassword" 
        android:padding="8dp"
        android:textColor="#000"
        android:textStyle="bold"
        android:textColorHint="#bdbdbd"
        android:textSize="13dp" />



     <Button
        android:id="@+id/button1"
        android:layout_width="70dp"
        android:layout_height="35dp"
       android:layout_marginRight="20dp"
        android:layout_marginTop="190dp"
        android:background="@drawable/loginbutton"
        android:ems="7"
        android:text="Login"
        android:textColor="#fff"
        android:textSize="12dp" />



</LinearLayout>
<LinearLayout
    android:id="@+id/panelInsurance7"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_marginLeft="15dp"
    android:layout_marginTop="10dp"
    >
  <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="225dp"
        android:text="Forget Your Password?"

        android:textSize="13dp"
        android:textStyle="bold" />

  </LinearLayout>

<LinearLayout
    android:id="@+id/linear1"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_marginLeft="15dp"
    android:layout_marginTop="10dp">

  <TextView
        android:id="@+id/textView2"
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="280dp"
        android:padding="10dp"
        android:layout_marginRight="20dp"
        android:text="Lorem Ipsum dolor sit amet,"
        android:background="@drawable/loginedit"
        android:textSize="18dp"
        android:textStyle="bold" />

  </LinearLayout>

</RelativeLayout>

Я хочу перекрестную кнопку, как на изображении ниже. Нажмите здесь


person sunil y    schedule 02.11.2015    source источник
comment
Возможный дубликат настраиваемого диалогового окна с кнопкой закрытия   -  person camelCaseCoder    schedule 02.11.2015
comment
Вы не определили относительное положение linearlayout ниже представления изображения в activity_login_page.xml   -  person bond007    schedule 02.11.2015
comment
@camelcasecoder... даже я столкнулся с той же проблемой... пока не нашел решения... это вы публикуете здесь.   -  person sunil y    schedule 02.11.2015
comment
@ Ноль, я тебя не понимаю.   -  person sunil y    schedule 02.11.2015
comment
Будет лучше, если вы разместите точное изображение того, что вы хотите.   -  person DroidAks    schedule 02.11.2015


Ответы (1)


Попробуйте это решение:

    <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"
        android:background="#fff"
        tools:context="com.example.miiskyproject.Login" >

    <LinearLayout
        android:id="@+id/cancel_layuot"
        android:layout_width="match_parent"
        android:gravity="right"
        android:background="@android:color/transparent"
        android:layout_height="wrap_content" >

        <ImageView
            android:id="@+id/cancel_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:src="@drawable/ic_launcher" />
    </LinearLayout>

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="130dp"
        android:layout_height="90dp"
        android:layout_below="@+id/cancel_layuot"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="34dp"
        android:src="@drawable/left_arrow" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_below="@+id/cancel_layuot" >

        <EditText
            android:id="@+id/editText1"
            android:layout_width="80dp"
            android:layout_height="35dp"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="10dp"
            android:layout_marginTop="190dp"
            android:background="@drawable/ic_launcher"
            android:ems="6"
            android:hint="User Name"
            android:padding="8dp"
            android:textColor="#000"
            android:textColorHint="#bdbdbd"
            android:textSize="13dp"
            android:textStyle="bold" >

            <requestFocus />
        </EditText>

        <EditText
            android:id="@+id/editText2"
            android:layout_width="80dp"
            android:layout_height="35dp"
            android:layout_marginRight="15dp"
            android:layout_marginTop="190dp"
            android:background="@drawable/ic_launcher"
            android:ems="6"
            android:hint="Password"
            android:inputType="textPassword"
            android:padding="8dp"
            android:textColor="#000"
            android:textColorHint="#bdbdbd"
            android:textSize="13dp"
            android:textStyle="bold" />

        <Button
            android:id="@+id/button1"
            android:layout_width="70dp"
            android:layout_height="35dp"
            android:layout_marginRight="20dp"
            android:layout_marginTop="190dp"
            android:background="@drawable/ic_launcher"
            android:ems="7"
            android:text="Login"
            android:textColor="#fff"
            android:textSize="12dp" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/panelInsurance7"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_marginLeft="15dp"
        android:layout_marginTop="10dp" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="225dp"
            android:text="Forget Your Password?"
            android:textSize="13dp"
            android:textStyle="bold" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/linear1"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_below="@+id/cancel_layuot"
        android:layout_marginLeft="15dp"
        android:layout_marginTop="10dp" >

        <TextView
            android:id="@+id/textView2"
            android:layout_width="250dp"
            android:layout_height="wrap_content"
            android:layout_marginRight="20dp"
            android:layout_marginTop="280dp"
            android:background="@drawable/ic_launcher"
            android:padding="10dp"
            android:text="Lorem Ipsum dolor sit amet,"
            android:textSize="18dp"
            android:textStyle="bold" />
    </LinearLayout>

</RelativeLayout>

Вы увидите что-то вроде этого:

введите здесь описание изображения

Просто замените изображение ic_launcher соответствующим изображением.

person activesince93    schedule 02.11.2015
comment
в эмуляторе даже я получаю такое представление ... но когда я запускаю свое физическое устройство, оно отображается только внутри. нужно ли мне что-то изменить в моем HomePage.java ..? - person sunil y; 02.11.2015
comment
убедитесь, что вы не меняете представление программно. - person activesince93; 02.11.2015
comment
Я отредактировал ответ. Пожалуйста, попробуйте заменить свой код кодом выше. Это должно работать. - person activesince93; 02.11.2015
comment
Я добавил cancel_btn в LinearLayout. - person activesince93; 02.11.2015
comment
Я вставил ваш код в свой проект. Но тот же результат.. он отображается внутри себя. Вы проверили физическое устройство..? - person sunil y; 02.11.2015