【问题标题】:How to set imageButton to top left corner of a subview (webview) in Android如何在Android中将imageButton设置为子视图(webview)的左上角
【发布时间】: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 正如我在“我不知道如何使用布局”问题中所写的那样

标签: android android-layout webview android-relativelayout android-imagebutton


【解决方案1】:

下面的xml代码可以做到:

<RelativeLayout 
 android:layout_width="match_parent"
android:layout_height="match_parent"
>

<ImageButton
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:layout_alignParentTop="true"
    />
<WebView
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>
</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 视图应该填充布局。我希望这会奏效。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-07
  • 2015-09-22
相关资源
最近更新 更多