【问题标题】:Android - Popup window creation and stylingAndroid - 弹出窗口的创建和样式
【发布时间】:2016-01-13 05:18:26
【问题描述】:

我正在尝试创建一个弹出窗口,其中包含多个RelativeLayout、多个TextView 和一个Button。

这是弹出窗口:

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

    <RelativeLayout
        android:id="@+id/popup_handphone_Wrapper"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/popup_handphone_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/popupPhoneMessage"/>

        <RelativeLayout
            android:id="@+id/popup_handphone_functionalities"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/popup_handphone_text"
            android:layout_marginTop="15dp">

            <EditText
                android:id="@+id/popup_handphone_phoneNumber"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

            <EditText
                android:id="@+id/popup_handphone_phoneNumberConfirm"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/popup_handphone_phoneNumber"
                android:layout_marginTop="10dp"/>

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/popup_handphone_phoneNumberConfirm"
                android:layout_marginTop="20dp"
                android:layout_marginBottom="40dp"/>

        </RelativeLayout>

    </RelativeLayout>

</RelativeLayout>

如何在Activity 上显示它?尝试使用LayoutInflater,但出现错误,提示它找到了View..

代码:

RelativeLayout mainLayout = (RelativeLayout)findViewById(R.id.popup_handphone_MainLayout);

        LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        RelativeLayout popupLayoutInflater = inflater.inflate(R.layout.popup_handphone, mainLayout);

//This part here said it needs android.widget.RelativeLayout, but found android.view.View

        popupLayout = (RelativeLayout)findViewById(R.layout.popup_handphone);

这是我想要的弹出窗口。灰色的是包含两个TextViews 和一个Button 的弹出窗口

设法使用此tutorial 创建一个弹出窗口。但是弹窗周围有黑色背景 像这样

这是TextView 上onclick 的更新代码

View.OnClickListener phoneReinputHandler = new View.OnClickListener() {

        public void onClick(View v) {
            Intent intent = new Intent(SignupStepTwoActivity.this, PopupHandphone.class);
            backDim = (RelativeLayout) findViewById(R.id.bac_dim_layout);
            //backDim.setVisibility(View.VISIBLE);
            startActivity(intent);

    };

如何去除黑色的东西?

【问题讨论】:

  • 你的代码和logcat是必须要回答的..
  • 是的,添加了,忘记添加了
  • 您是否尝试过将父级 relativelayout 背景设为透明?
  • 是的,我试过了,似乎不是相对背景造成的。这可能是主题吗?不过,我现在正在使用PopupWindow,正在努力通过触摸窗外来解除它
  • 可能是因为他们,我从来没有在 popupwindow 上工作过所以不太确定,但是您提供的设计可以使用对话框轻松创建

标签: android


【解决方案1】:

查看此内容并根据您的需要进行修改

public void showDialog()
{
    final Dialog dialog = new Dialog(mContext, android.R.style.ThemeOverlay_Material);/*choose your theme*/
    dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));/*choose your style*/
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);/*to remove title*/
         dialog.getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT); //Optional
    dialog.setContentView(R.layout.dialog_dash_wish);/*enter your xml layout name*/
    //Any other code you want in your constructor

    TextView popup_handphone_text = (TextView) dialog.findViewById(R.id.popup_handphone_text);
    EditText popup_handphone_phoneNumber = (EditText) dialog.findViewById(R.id.popup_handphone_phoneNumber);

    EditText popup_handphone_phoneNumberConfirm = (EditText) dialog.findViewById(R.id.popup_handphone_phoneNumberConfirm);

    Button button = (Button) dialog.findViewById(R.id.bt_id);
    button .setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View arg0)
        {
            dialog.dismiss();
        }
    });
    dialog.show();
}

【讨论】:

  • 嗯抱歉这是给Dialog的吗?我需要PopupWindow.. AlertDialog 可以像PopupWindow 一样使用吗?
  • 它也可以用作弹出窗口,您可以发布您实际想要实现的图形表示吗?这肯定会帮助我们可视化您的要求
猜你喜欢
  • 2012-07-17
  • 1970-01-01
  • 2014-05-26
  • 1970-01-01
  • 1970-01-01
  • 2011-12-16
  • 2021-10-07
  • 1970-01-01
相关资源
最近更新 更多