Webview in programmatically created RelativeLayout

前端 未结 1 809
陌清茗
陌清茗 2020-12-03 09:30

I need the following xml to be made in code:



        
相关标签:
1条回答
  • 2020-12-03 09:42

    You should post any exception you get in your question. See if this helps:

        RelativeLayout popwindow=new RelativeLayout(this);
        FrameLayout.LayoutParams rl= new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, 50);
        popwindow.setLayoutParams(rl);
        WebView w= new WebView(this);
        w.setId(0X100);
        w.setScrollContainer(false);
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, 50);
        params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        w.setLayoutParams(params);
        Button b=new Button(this);
        b.setId(0X101);
        b.setText("X");
        b.setBackgroundColor(Color.TRANSPARENT);
        RelativeLayout.LayoutParams bparams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, ReloativeLayout.LayoutParams.WRAP_CONTENT);
        bparams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        bparams.addRule(RelativeLayout.ALIGN_TOP, w.getId());
        b.setLayoutParams(bparams);        
        popwindow.addView(w);
        popwindow.addView(b);
        setContentView(popwindow);
    

    Custom component holding the WebView and the Button:

    public class CustomRelativeLayout extends RelativeLayout {
    
            private WebView mWebView;
            private Button mButton;
    
            public CustomRelativeLayout(Context context) {
                super(context);
                mWebView = new WebView(context);
                mWebView.setId(0X100);
                mWebView.setScrollContainer(false);
                RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                        RelativeLayout.LayoutParams.FILL_PARENT, 50);
                params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
                mWebView.setLayoutParams(params);
                mButton = new Button(context);
                mButton.setId(0X101);
                mButton.setText("X");
                mButton.setBackgroundColor(Color.TRANSPARENT);
                RelativeLayout.LayoutParams bparams = new RelativeLayout.LayoutParams(
                        RelativeLayout.LayoutParams.WRAP_CONTENT,
                        RelativeLayout.LayoutParams.WRAP_CONTENT);
                bparams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
                bparams.addRule(RelativeLayout.ALIGN_TOP, mWebView.getId());
                mButton.setLayoutParams(bparams);
                addView(mWebView);
                addView(mButton);
            }
    
            public WebView getTheWebView() {
                return mWebView;
            }
    
            public Button getTheButton() {
                return mButton;
            }
    
        }
    

    To use that component:

    CustomRelativeLayout popWindow = new CustomRelativeLayout(this);
        FrameLayout.LayoutParams rl= new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, 50);
    setContentView(popWindow);
    // get a reference to the WebView and the Button
    WebView w = popWindow.getTheWebView();
    Button b = popWindow.getTheButton(); 
    
    0 讨论(0)
提交回复
热议问题