Android 以编程方式创建的相对布局中的 Webview

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11221777/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:22:43  来源:igfitidea点击:

Webview in programmatically created RelativeLayout

androidandroid-layoutandroid-webview

提问by Shalini

I need the following xml to be made in code:

我需要在代码中创建以下 xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:orientation="vertical" android:gravity="right"
            android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#00000000"
            >
            <android.webkit.WebView android:layout_width="fill_parent"
            android:id="@+id/adview"
            android:scrollbars="none"
            android:layout_height="50dp"
            android:layout_alignParentLeft="true"
            android:layout_alignParentBottom="true"
            ></android.webkit.WebView>
            <Button android:layout_width="wrap_content" android:text="x" android:textColor="#000000" 
            android:layout_height="wrap_content" android:id="@+id/hide_popup_button" android:background="#00000000" 
            android:layout_alignTop="@+id/adview" android:layout_alignParentRight="true"></Button>

        </RelativeLayout> 

I use the code below but I get a Force close:

我使用下面的代码,但我得到一个Force close

RelativeLayout popwindow=new RelativeLayout(this);

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

I need suggestions on how to make that xml layout through java code.

我需要有关如何通过 Java 代码制作该 xml 布局的建议。

采纳答案by Luksprog

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 WebViewand the Button:

包含WebView和 的自定义组件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();