android - 以编程方式创建的 RelativeLayout 中的 Webview

标签 android android-layout android-webview

我需要在代码中制作以​​下 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> 

我使用下面的代码,但我得到了一个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);

我需要关于如何通过 java 代码制作 xml 布局的建议。

最佳答案

您应该发布您在问题中遇到的任何异常。看看这是否有帮助:

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

包含 WebViewButton 的自定义组件:

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

关于android - 以编程方式创建的 RelativeLayout 中的 Webview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11221777/

相关文章:

android - 避免冗余构建以制作 tessaract android 库 && 将 tessaract 库链接到 c++ NDK

c# - 将布局保存到位图(Xamarin)

android - 透明按钮周围的边框

android - 在 XML 布局 android 中使用带占位符的字符串

java - 将 XML 数据(文件)绑定(bind)到 android 中的 GridView ?

android - 以编程方式检索用户代理

java - Android UTF-8 编码不起作用?

java - 评估匿名类的 toString() 时出现 NullPointerException

javascript - 如何确定页面是否在嵌入式浏览器中打开

android - WebView 中的 IndexOutOfBoundsException