在弹出的窗口的Andr​​oid设置自己的布局

在弹出的窗口的Andr​​oid设置自己的布局

本文介绍了在弹出的窗口的Andr​​oid设置自己的布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个弹出式窗口的一个问题。我想创建弹出窗口,我自己的布局。这是code:

 公共类PopupWindowView扩展PopupWindow {

    PopupWindow弹出;
    布尔点击= TRUE;
    的LayoutParams PARAMS;
    RelativeLayout的mainLayout;
    TextView的电视;
    的LinearLayout布局;
    ImageView的chooseFlag;

    公共无效createPopupWindow(活动行为){
        弹出=新PopupWindow(行为);
        chooseFlag =(ImageView的)act.findViewById(R.id.login_choose_flag);
        mainLayout =(RelativeLayout的)act.findViewById(R.id.login_layout);
        电视=新的TextView(行为);
        布局=新的LinearLayout(行为);
        //布局=(的LinearLayout)findViewById(R.id.popuplayout);
        chooseFlag.setOnClickListener(新OnClickListener(){

            @覆盖
            公共无效的onClick(视图v){
                如果(点击进入){
                    INT []值=新INT [2];
                    v.getLocationOnScreen(值);
                    popup.showAtLocation(mainLayout,Gravity.NO_GRAVITY,10,10);
                    popup.update(值[0],值[1],300,80);
                    点击= FALSE;
                } 其他 {
                    popup.dismiss();
                    点击= TRUE;
                }
            }
        });
        PARAMS =新的LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT);
        layout.setOrientation(LinearLayout.VERTICAL);
        tv.setText(你好,这是一个简单的文本弹出窗口);
        layout.addView(电视,则params);
        popup.setContentView(布局);
    }
}
 

这是布局,我想在我的弹出式窗口设置:

 < XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
   >

    <的LinearLayout
        机器人:ID =@ + ID / popuplayout
        机器人:layout_width =match_parent
        机器人:layout_height =match_parent
        机器人:背景=@色/ patient_button_bg>
    < / LinearLayout中>

< / LinearLayout中>
 

在我的课,我不能使用 findbyid 的方法,因为这不是活动。我如何设置,在弹出的遗孀我自己的布局在我的课?

编辑:这是堆栈跟踪在那里我得到错误:

  9月3号至1日:48:48.761:E / AndroidRuntime(16776):致命异常:主要
9月3日至1日:48:48.761:E / AndroidRuntime(16776):java.lang.IllegalStateException:指定的孩子已经有一个父。你必须先调用removeView()对孩子的家长。
9月3日至1日:48:48.761:E / AndroidRuntime(16776):在android.view.ViewGroup.addViewInner(ViewGroup.java:3337)
9月3日至1日:48:48.761:E / AndroidRuntime(16776):在android.view.ViewGroup.addView(ViewGroup.java:3208)
9月3日至1日:48:48.761:E / AndroidRuntime(16776):在android.view.ViewGroup.addView(ViewGroup.java:3188)
9月3日至1日:48:48.761:E / AndroidRuntime(16776):在android.widget.PopupWindow preparePopup(PopupWindow.java:969)
9月3日至1日:48:48.761:E / AndroidRuntime(16776):在android.widget.PopupWindow.showAtLocation(PopupWindow.java:840)
9月3日至1日:48:48.761:E / AndroidRuntime(16776):在android.widget.PopupWindow.showAtLocation(PopupWindow.java:813)
9月3日至1日:48:48.761:E / AndroidRuntime(16776):在pl.asseco.amms.mobile.tools.PopupWindowView $ 1.onClick(PopupWindowView.java:44)
9月3日至1日:48:48.761:E / AndroidRuntime(16776):在android.view.View.performClick(View.java:3558)
9月3日至1日:48:48.761:E / AndroidRuntime(16776):在android.view.View $ PerformClick.run(View.java:14152)
9月3日至1日:48:48.761:E / AndroidRuntime(16776):在android.os.Handler.handleCallback(Handler.java:605)
9月3日至1日:48:48.761:E / AndroidRuntime(16776):在android.os.Handler.dispatchMessage(Handler.java:92)
9月3日至1日:48:48.761:E / AndroidRuntime(16776):在android.os.Looper.loop(Looper.java:137)
9月3日至1日:48:48.761:E / AndroidRuntime(16776):在android.app.ActivityThread.main(ActivityThread.java:4514)
9月3日至1日:48:48.761:E / AndroidRuntime(16776):在java.lang.reflect.Method.invokeNative(本机方法)
9月3日至1日:48:48.761:E / AndroidRuntime(16776):在java.lang.reflect.Method.invoke(Method.java:511)
9月3日至1日:48:48.761:E / AndroidRuntime(16776):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:790)
9月3日至1日:48:48.761:E / AndroidRuntime(16776):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
9月3日至1日:48:48.761:E / AndroidRuntime(16776):在dalvik.system.NativeStart.main(本机方法)
 

编辑活动其中使用弹出窗口:

  @覆盖
保护无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_login);
    getActionBar()隐藏()。

    mainMenuGenerator =新MainMenuGenerator();
    mainMenuGenerator.generateMainMenu(本);
    mainMenuGenerator.hideIcons();
    弹出=新PopupWindowView();

    popup.createPopupWindow(本);

}
 

解决方案

试试这个code:

 私人无效showSortPopup(最后的活动范围内,点P)
{
       //膨胀popup_layout.xml
       LinearLayout中的ViewGroup =(的LinearLayout)context.findViewById(R.id.llSortChangePopup);
       LayoutInflater layoutInflater =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
       查看布局= layoutInflater.inflate(R.layout.sort_popup_layout,ViewGroup中);

       //创建PopupWindow
       changeSortPopUp =新PopupWindow(上下文);
       changeSortPopUp.setContentView(布局);
       changeSortPopUp.setWidth(LinearLayout.LayoutParams.WRAP_CONTENT);
       changeSortPopUp.setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);
       changeSortPopUp.setFocusable(真正的);

       //有些偏移对准弹出有点向左,有点下降,相对于按钮的位置。
       INT OFFSET_X = -20;
       INT OFFSET_Y = 95;

       //清除默认的半透明背景
       changeSortPopUp.setBackgroundDrawable(新BitmapDrawable());

       //显示弹出指定位置,+偏移。
       changeSortPopUp.showAtLocation(布局,Gravity.NO_GRAVITY,PX + OFFSET_X,PY + OFFSET_Y);


       //获取一个参考关闭按钮,点击后关闭弹出窗口。
       按钮,关闭=(按钮)layout.findViewById(R.id.close);
       close.setOnClickListener(新OnClickListener(){

         @覆盖
         公共无效的onClick(视图v){
           popup.dismiss();
         }
       });

}
 

I have a problem with popup window. I want to create popup window with my own layout.This is code:

public class PopupWindowView extends PopupWindow{

    PopupWindow popup;
    boolean click = true;
    LayoutParams params;
    RelativeLayout mainLayout;
    TextView tv;
    LinearLayout layout;
    ImageView chooseFlag;

    public void createPopupWindow(Activity act){
        popup = new PopupWindow(act);
        chooseFlag = (ImageView) act.findViewById(R.id.login_choose_flag);
        mainLayout = (RelativeLayout) act.findViewById(R.id.login_layout);
        tv = new TextView(act);
        layout = new LinearLayout(act);
        //layout = (LinearLayout) findViewById(R.id.popuplayout);
        chooseFlag.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (click) {
                    int[] values = new int[2];
                    v.getLocationOnScreen(values);
                    popup.showAtLocation(mainLayout, Gravity.NO_GRAVITY, 10, 10);
                    popup.update(values[0], values[1], 300, 80);
                    click = false;
                } else {
                    popup.dismiss();
                    click = true;
                }
            }
        });
        params = new LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT);
        layout.setOrientation(LinearLayout.VERTICAL);
        tv.setText("Hi this is a sample text for popup window");
        layout.addView(tv, params);
        popup.setContentView(layout);
    }
}

and this is layout which I want to set in my popup window:

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

    <LinearLayout
        android:id="@+id/popuplayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/patient_button_bg">
    </LinearLayout>

</LinearLayout>

In my class I can't use findbyid method because this is not Activity. How I can set my own layout in popup widow in my class?

Edit:this is stack trace where I get error:

03-01 09:48:48.761: E/AndroidRuntime(16776): FATAL EXCEPTION: main
03-01 09:48:48.761: E/AndroidRuntime(16776): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
03-01 09:48:48.761: E/AndroidRuntime(16776):    at android.view.ViewGroup.addViewInner(ViewGroup.java:3337)
03-01 09:48:48.761: E/AndroidRuntime(16776):    at android.view.ViewGroup.addView(ViewGroup.java:3208)
03-01 09:48:48.761: E/AndroidRuntime(16776):    at android.view.ViewGroup.addView(ViewGroup.java:3188)
03-01 09:48:48.761: E/AndroidRuntime(16776):    at android.widget.PopupWindow.preparePopup(PopupWindow.java:969)
03-01 09:48:48.761: E/AndroidRuntime(16776):    at android.widget.PopupWindow.showAtLocation(PopupWindow.java:840)
03-01 09:48:48.761: E/AndroidRuntime(16776):    at android.widget.PopupWindow.showAtLocation(PopupWindow.java:813)
03-01 09:48:48.761: E/AndroidRuntime(16776):    at pl.asseco.amms.mobile.tools.PopupWindowView$1.onClick(PopupWindowView.java:44)
03-01 09:48:48.761: E/AndroidRuntime(16776):    at android.view.View.performClick(View.java:3558)
03-01 09:48:48.761: E/AndroidRuntime(16776):    at android.view.View$PerformClick.run(View.java:14152)
03-01 09:48:48.761: E/AndroidRuntime(16776):    at android.os.Handler.handleCallback(Handler.java:605)
03-01 09:48:48.761: E/AndroidRuntime(16776):    at android.os.Handler.dispatchMessage(Handler.java:92)
03-01 09:48:48.761: E/AndroidRuntime(16776):    at android.os.Looper.loop(Looper.java:137)
03-01 09:48:48.761: E/AndroidRuntime(16776):    at android.app.ActivityThread.main(ActivityThread.java:4514)
03-01 09:48:48.761: E/AndroidRuntime(16776):    at java.lang.reflect.Method.invokeNative(Native Method)
03-01 09:48:48.761: E/AndroidRuntime(16776):    at java.lang.reflect.Method.invoke(Method.java:511)
03-01 09:48:48.761: E/AndroidRuntime(16776):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
03-01 09:48:48.761: E/AndroidRuntime(16776):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
03-01 09:48:48.761: E/AndroidRuntime(16776):    at dalvik.system.NativeStart.main(Native Method)

EditActivity which use popup:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);
    getActionBar().hide();

    mainMenuGenerator = new MainMenuGenerator();
    mainMenuGenerator.generateMainMenu(this);
    mainMenuGenerator.hideIcons();
    popup = new PopupWindowView();

    popup.createPopupWindow(this);

}
解决方案

try this code:

private void showSortPopup(final Activity context, Point p)
{
       // Inflate the popup_layout.xml
       LinearLayout viewGroup = (LinearLayout) context.findViewById(R.id.llSortChangePopup);
       LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
       View layout = layoutInflater.inflate(R.layout.sort_popup_layout, viewGroup);

       // Creating the PopupWindow
       changeSortPopUp = new PopupWindow(context);
       changeSortPopUp.setContentView(layout);
       changeSortPopUp.setWidth(LinearLayout.LayoutParams.WRAP_CONTENT);
       changeSortPopUp.setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);
       changeSortPopUp.setFocusable(true);

       // Some offset to align the popup a bit to the left, and a bit down, relative to button's position.
       int OFFSET_X = -20;
       int OFFSET_Y = 95;

       // Clear the default translucent background
       changeSortPopUp.setBackgroundDrawable(new BitmapDrawable());

       // Displaying the popup at the specified location, + offsets.
       changeSortPopUp.showAtLocation(layout, Gravity.NO_GRAVITY, p.x + OFFSET_X, p.y + OFFSET_Y);


       // Getting a reference to Close button, and close the popup when clicked.
       Button close = (Button) layout.findViewById(R.id.close);
       close.setOnClickListener(new OnClickListener() {

         @Override
         public void onClick(View v) {
           popup.dismiss();
         }
       });

}

这篇关于在弹出的窗口的Andr​​oid设置自己的布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-04 22:05