web视图的编程方式创建RelativeLayout的 [英] Webview in programmatically created RelativeLayout

查看:115
本文介绍了web视图的编程方式创建RelativeLayout的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要以下XML在code,以进行:

I need the following xml to be made in code:

<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> 

我用下面的code,但我得到了强制关闭

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);

我需要就如何使通过Java code表示XML布局的建议。

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

推荐答案

您应该张贴在你的问题得到任何的异常。看看这会有所帮助:

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);

自定义组件持有的WebView 按钮

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;
        }

    }

要使用该组件:

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(); 

这篇关于web视图的编程方式创建RelativeLayout的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆