简体   繁体   English

PopUpWindow显示不正确

[英]PopUpWindow displays incorrectly

I am trying to replicate this layout of this view (not necessarily the rounded rectangles of the editText boxes, just the widths/layout): 我正在尝试复制此视图的这种布局(不一定是editText框的圆角矩形,只是宽度/布局):

在此处输入图片说明

The dimmed background color, the editText's widths. 变暗的背景色,editText的宽度。 However, I'm getting this: 但是,我得到这个:

在此处输入图片说明

No transparent background color, the user can access the view behind the popup and the editTexts are shrunk. 没有透明的背景色,用户可以访问弹出窗口后面的视图,并且editTexts缩小。 This popupwindow is being called from a fragment. 该popupwindow是从片段中调用的。

1. I do not want the user to be able to touch the calling fragment so I used: 1.我不希望用户能够触摸呼叫片段,所以我使用了:

mPopupWindow.setOutsideTouchable(false);

But the user can still access the view behind the popup. 但是用户仍然可以访问弹出窗口后面的视图。 And I thought it was because the background hadn't been set yet. 我认为这是因为尚未设置背景。 So I did this: 所以我这样做:

Here are the relevant pieces of code (the full code is at the bottom). 这是相关的代码段(完整的代码在底部)。

2. For the background color: 2.对于背景色:

In my colors.xml I have this: 在我的colors.xml中,我有以下内容:

 < color name="COLOR_50_GREY" >#80636363</color> 

According to this SO question you can create transparency. 根据这个SO问题,您可以创建透明度。 But I have also tried standard colors too. 但是我也尝试过标准颜色。

I called that color (or the android default colors) with : 我用以下方式调用了该颜色(或android默认颜色):

mPopupWindow.setBackgroundDrawable(new ColorDrawable(ContextCompat.getColor(getContext(), R.color.COLOR_50_GREY)));

but no luck. 但没有运气。

3. Shrunk text views. 3.缩小文本视图。 According to this SO question's second answer 根据这个SO问题的第二个答案

I should be able create a weighted LinearLayout using the below ( Obvious stuff removed for clarity , full code at the bottom): 我应该可以使用以下内容创建加权的LinearLayout( 为了清楚起见删除了明显的内容 ,底部是完整代码):

 <LinearLayout
    android:orientation="horizontal"
    android:weightSum="5">

    <TextView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"/>

    <EditText
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="3"/>

    <Button
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"/>

</LinearLayout>

But as you can see from the screenshot above, the weights are not honored. 但是,如您从上面的屏幕截图中所见,权重不被接受。

Any help? 有什么帮助吗?

How to call my popup from in my fragment 如何从片段中调用我的弹出窗口

private void showCompeletPopup() {

        LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View customView = inflater.inflate(R.layout.popup_close_ticket, null);

        mPopupWindow = new PopupWindow(customView, LinearLayoutCompat.LayoutParams.MATCH_PARENT, LinearLayoutCompat.LayoutParams.WRAP_CONTENT);

        // Set an elevation value for popup window call requires API level 21
        if (Build.VERSION.SDK_INT >= 21) {
            mPopupWindow.setElevation(5.0f);
        }

        mPopupWindow.setOutsideTouchable(false);
        mPopupWindow.setBackgroundDrawable(new ColorDrawable(ContextCompat.getColor(getContext(), R.color.COLOR_50_GREY)));

        mPopupWindow.showAtLocation(getView(), Gravity.CENTER, 0, 0);
}

Popup windows XML 弹出窗口XML

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/COLOR_LIGHT_GREY"
            android:padding="8dp"
            android:layout_margin="20dp"
            android:scrollbars="none">

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


        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_marginBottom="5dp"
            android:orientation="horizontal"
            android:padding="3dp"
            android:weightSum="5">

            <TextView
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_marginEnd="4dp"
                android:layout_weight="1"
                android:text="TOW TICKET:"
                android:gravity="right|center_vertical"
                android:textAllCaps="true"
                android:textColor="@color/COLOR_BLUE"
                android:textIsSelectable="false"
                android:textStyle="bold"/>


            <EditText
                android:id="@+id/etTowTicketNumber"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="3"
                android:imeOptions="actionDone"
                android:inputType="text"
                android:maxLength="30"
                android:maxLines="1"
                />

            <Button
                android:id="@+id/btnNA"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_gravity="right"
                android:layout_weight="1"
                android:background="@color/COLOR_BLUE"
                android:padding="1dp"
                android:text="NA"
                android:textColor="@color/COLOR_WHITE"
                android:textSize="18sp"
                android:textStyle="bold"/>

        </LinearLayout>


//OTHER STUFF TRUNCATED FOR BREVITY 

    </LinearLayout>

</ScrollView>

You need to make custom drawable design for your edit text and set it as edit text background then you can achieve 您需要为编辑文本进行自定义可绘制设计,并将其设置为编辑文本背景,然后才能实现

<?xml version="1.0" encoding="utf-8"?>
<!-- res/drawable/rounded_edittext.xml -->
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:padding="10dp"
    android:shape="rectangle" >

    <solid android:color="#FFFFFF" />

    <corners
        android:bottomLeftRadius="10dp"
        android:bottomRightRadius="10dp"
        android:topLeftRadius="10dp"
        android:topRightRadius="10dp" />

</shape>

And for giving hint as "Enter Value" use android:hint attribute 对于提示为“输入值”,请使用android:hint属性

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM