如何在android

时间:2016-03-16 13:01:20

标签: android android-relativelayout

我的底部有一个布局。尝试在此布局上方弹出一个对话框。参考屏幕截图enter image description here有人可以帮助我吗?

这是我的活动

    package com.example.dynamicviews;

    import android.app.Dialog;
    import android.graphics.Color;
    import android.os.Bundle;
    import android.support.design.widget.FloatingActionButton;
    import android.support.design.widget.Snackbar;
    import android.support.v4.view.ViewPager;
    import android.support.v7.app.AppCompatActivity;
    import android.support.v7.widget.Toolbar;
    import android.view.Gravity;
    import android.view.View;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.ViewGroup;
    import android.view.WindowManager;
    import android.widget.EditText;
    import android.widget.ImageView;
    import android.widget.RelativeLayout;
    import android.widget.TextView;

    public class MainActivity extends AppCompatActivity {

    RelativeLayout relativeLayoutSend;
    TextView txt_attach;
    EditText edit_reply;
    TextView txt_send;
    TextView txt_showattach;
    ViewPager pager;
    Dialog attachmentDialog;
    ImageView img_attachment;
    ImageView img_emailtemplate;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        pager=(ViewPager)findViewById(R.id.pager);


        relativeLayoutSend=(RelativeLayout)findViewById(R.id.relativeLayoutSend);

        RelativeLayout.LayoutParams params_attach=new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        params_attach.leftMargin=20;
        txt_attach=new TextView(this);
        txt_attach.setId(R.id.relativeLayoutSend+1);
        txt_attach.setBackgroundResource(R.mipmap.ic_launcher);
        txt_attach.setLayoutParams(params_attach);
        params_attach.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
        params_attach.addRule(RelativeLayout.CENTER_VERTICAL);
        relativeLayoutSend.addView(txt_attach);

        RelativeLayout.LayoutParams params_reply=new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        edit_reply=new EditText(this);
        edit_reply.setEms(13);
        edit_reply.setLayoutParams(params_reply);
        params_reply.addRule(RelativeLayout.CENTER_IN_PARENT);
        relativeLayoutSend.addView(edit_reply);

        RelativeLayout.LayoutParams params_send=new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        params_send.rightMargin=20;
        txt_send=new TextView(this);
        txt_send.setBackgroundResource(R.mipmap.ic_launcher);
        txt_send.setLayoutParams(params_send);
        params_send.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        params_send.addRule(RelativeLayout.CENTER_VERTICAL);
        relativeLayoutSend.addView(txt_send);

        RelativeLayout.LayoutParams params_showattach=new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        params_showattach.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        params_attach.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
        txt_showattach=new TextView(this);
        params_showattach.leftMargin=40;
        txt_showattach.setLayoutParams(params_showattach);
        relativeLayoutSend.addView(txt_showattach);


        txt_attach.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                attachmentDialog = new Dialog(MainActivity.this, R.style.PauseDialog);
                attachmentDialog.setContentView(R.layout.custom_attachment);
                attachmentDialog.setCancelable(true);
                img_attachment = (ImageView) attachmentDialog.findViewById(R.id.img_attachment);
                img_emailtemplate = (ImageView) attachmentDialog.findViewById(R.id.img_template);

                attachmentDialog.getWindow().getAttributes().gravity=Gravity.BOTTOM|Gravity.LEFT;

                WindowManager.LayoutParams params=attachmentDialog.getWindow().getAttributes();
                params.y=300;
                attachmentDialog.getWindow().setAttributes(params);
                attachmentDialog.show();

            }
        });


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

这是我的xml

custom_attachment.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/popup_element"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#0054ae"
android:layout_marginLeft="16sp">

<ImageView
    android:id="@+id/img_attachment"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@mipmap/ic_launcher"
    />

<ImageView
    android:id="@+id/img_template"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@mipmap/ic_launcher"
    android:layout_toRightOf="@+id/img_attachment"
    android:layout_marginLeft="16sp" />

</RelativeLayout>

activity_main.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"
android:fitsSystemWindows="true"
tools:context="com.example.dynamicviews.MainActivity">

<RelativeLayout
    android:id="@+id/relativeLayoutSend"
    android:layout_width="match_parent"
    android:layout_height="75sp"
    android:layout_alignParentBottom="true"
    android:background="#0054ae">

</RelativeLayout>

我需要设置params.above params.y = 300,而不是设置relativeLayoutSend对齐不同设备的对齐方式。我怎么能这样做?

1 个答案:

答案 0 :(得分:0)

Builder类将帮助您完成此任务。

示例:

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(urlayout);
builder.setTitle("urtitle");
alertDialog = builder.create();
alertDialog.getWindow().setLayout(600, 400); //Controlling width and height.
alertDialog.show();

当您获得窗口然后设置布局宽度和高度时,它不应该影响。

显示屏幕底部的对话框。

示例:

Dialog dialogue = <your custom dialog>;

Window window = dialogue.getWindow();
WindowManager.LayoutParams layoutparam = window.getAttributes();

layoutparam .gravity = Gravity.BOTTOM;
layoutparam .flags &= ~WindowManager.LayoutParams.FLAG_DIM_BEHIND;
window.setAttributes(layoutparam );