我在设置imageButton到我的WebView的左上角时遇到了麻烦,我不知道如何使用布局(线性或相对),我尝试了很多代码块,但没有一个能为我工作
这是我创建imageButton的方式;
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);
关于如何在不使用xml的情况下正确放置它的任何想法,因为我正在尝试为将要分发的jar文件执行此操作
答案 0 :(得分:0)
以下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>
答案 1 :(得分:0)
我认为你应该把你的观点放在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);
答案 2 :(得分:0)
试试这个:
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控件