如何在Android中将imageButton设置为子视图(webview)的左上角

Posted

技术标签:

【中文标题】如何在Android中将imageButton设置为子视图(webview)的左上角【英文标题】:How to set imageButton to top left corner of a subview (webview) in Android 【发布时间】:2016-01-21 17:05:32 【问题描述】:

我在将 imageButton 设置为 WebView 的左上角时遇到问题,我不知道如何使用布局(线性或相对),我尝试了很多代码块,但没有一个对我有用.

这就是我创建图像按钮的方式;

ImageButton mImageButton = new ImageButton(context);
        mImageButton.setImageResource(R.drawable.close_button);
        mImageButton.setLayoutParams(new ViewGroup.LayoutParams(width, height));
        mImageButton.setAdjustViewBounds(true);
        mImageButton.setBackgroundDrawable(null);
        mImageButton.setScaleType(ImageView.ScaleType.FIT_CENTER);

这就是我创建图像按钮并将其添加到 webview 的方式

        WebView mWebView = new WebView(context);
        mWebView.loadData(htmlContent, "text/html", "UTF-8");
        mWebView.addView(mImageButton);

This is how it looks like

关于如何在不使用 xml 的情况下正确放置它的任何想法,因为我正在尝试为将要分发的 jar 文件执行此操作

【问题讨论】:

为什么不用xml中的布局文件? @sonic 哦,我应该在问题中说,我正在创建一个将用于其他人的 jar 文件,所以这不是一个选项 根据图片你可以使用这种类型的视图布局 @Naveen 是对的,您可以使用框架布局并在该框架布局中放置 webview 和 imageview 如何@Harish 正如我在“我不知道如何使用布局”问题中所写的那样 【参考方案1】:

下面的xml代码可以做到:

<RelativeLayout 
 android:layout_
android:layout_
>

<ImageButton
    android:layout_
    android:layout_
    android:layout_alignParentTop="true"
    />
<WebView
    android:layout_
    android:layout_/>
</RelativeLayout>

【讨论】:

由于问题中的解释原因,我无法使用 xml 文件【参考方案2】:

试试这个:

RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.mainLayout);

FrameLayout frameLayout = new FrameLayout(this);
frameLayout.setLayoutParams(
        new FrameLayout.LayoutParams(
                FrameLayout.LayoutParams.MATCH_PARENT,
                FrameLayout.LayoutParams.MATCH_PARENT));

View mWebView = new View(this);
mWebView.setBackgroundColor(Color.parseColor("#FF0000"));
mWebView.setLayoutParams(
        new ViewGroup.LayoutParams(
                FrameLayout.LayoutParams.MATCH_PARENT,
                FrameLayout.LayoutParams.MATCH_PARENT));

ImageButton mImageButton = new ImageButton(this);
mImageButton.setImageResource(android.R.drawable.star_big_on);
mImageButton.setLayoutParams(new ViewGroup.LayoutParams(200, 200));
mImageButton.setAdjustViewBounds(true);

frameLayout.addView(mWebView);
frameLayout.addView(mImageButton);

relativeLayout.addView(frameLayout);
在我的示例中,

relativeLayout 变量是添加所有元素的容器。此外,您应该用您的 WebView 替换我的 View 控件

【讨论】:

无法解析 findViewById 是我遇到的错误 @EvrenBaykan 在这种情况下我帮不了你。你不知道android的基础知识。对不起。【参考方案3】:

我认为您应该将您的观点放在RelativeLayout 中。所以我假设你得到一个RelativeLayout 对象。以下代码可能有效。 创建布局:

RelativeLayout mRelativeLayout = new RelativeLayout(context)

创建WebView 并将其添加到布局中:

WebView mWebView = new WebView(context);
RelativeLayout.LayoutParams paramsWV = new RelativeLayout.LayoutParams(mRelativeLayout.getWidth(), mRelativeLayout.getHeight);
mParamsWM.addRule(RelativeLayout.LayoutParams.FILL_PARENT);//so the web view fill the layout
mWebView.setLayoutParams(mParamsWM);
mWebView.loadData(htmlContent, "text/html", "UTF-8");
mRelativeLayout.addView(mWebView);

然后是按钮

ImageButton mImageButton = new ImageButton(context);
mImageButton.setImageResource(R.drawable.close_button);
RelativeLayout.LayoutParams paramsB = new RelativeLayout.LayoutParams(width, height);
paramsB.addRule(RelativeLayout.ALIGN_PARENT_TOP);
paramsB.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
mImageButton.setLayoutParams(paramsB);
mImageButton.setAdjustViewBounds(true);
mImageButton.setBackgroundDrawable(null);
mRelativeLayout.addView(mImageButton);
mImageButton.setScaleType(ImageView.ScaleType.FIT_CENTER);

【讨论】:

我没有在这个函数中使用的相对布局对象(mRelativeLayout) 然后你应该首先创建一个你想要使用的尺寸。这种布局将允许您随意放置视图。 你能帮忙创建布局吗? 我编辑我的答案以添加布局的创建。我还添加了这一行mParamsWM.addRule(RelativeLayout.LayoutParams.FILL_PARENT);,因此 Web 视图应该填充布局。我希望这会奏效。

以上是关于如何在Android中将imageButton设置为子视图(webview)的左上角的主要内容,如果未能解决你的问题,请参考以下文章

Android在Imagebutton上设置动态图像[关闭]

在 Android 中的 ImageButton 中适合图像

android 在代码中这是imagebutton的大小和间距。

如何将相对布局背景设置为透明

如何在水平LinearLayout中将元素右对齐?

android 给imageButton设置的图标太模糊。是啥原因?