我有一个附有点击监听器的表面视图。
当我点击它时,它将它缩小到宽度的一半和高度的一半......并通过以下方式将它放在右下角:
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
getWindowManager().getDefaultDisplay().getWidth()/2,
getWindowManager().getDefaultDisplay().getHeight()/2
);
rlp.setMargins(
getWindowManager().getDefaultDisplay().getWidth()/2,
getWindowManager().getDefaultDisplay().getHeight()/2,
0,
0);
它按预期工作,但我想在它后面动态地放置webView。
我试过了:
WebView wv = makeWebView();
//makeWebView(); is a method of my mainActivity, which simple does.
private Webview makeWebView(){ return new WebView(this); }
我似乎无法让它落后于其他一切。我做错了吗?
我的XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/RR1"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<LinearLayout
android:id="@+id/LL1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<SurfaceView
android:id="@+id/surfaceView1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</RelativeLayout>
我的MainActivity:
package com.example.testfortim;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.view.Gravity;
import android.view.Menu;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//SurfaceView sv = new SurfaceView();
SurfaceView sv = (SurfaceView)findViewById(R.id.surfaceView1);
sv.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
LinearLayout ll = (LinearLayout)findViewById(R.id.LL1);
//LinearLayout.LayoutParams llp = (LinearLayout.LayoutParams)ll.getLayoutParams();
//RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(ll.getLayoutParams().width/2,ll.getLayoutParams().height/2);//(RelativeLayout.LayoutParams)ll.getLayoutParams();
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
getWindowManager().getDefaultDisplay().getWidth()/2,
getWindowManager().getDefaultDisplay().getHeight()/2
);
rlp.setMargins(getWindowManager().getDefaultDisplay().getWidth()/2, getWindowManager().getDefaultDisplay().getHeight()/2, 0, 0);
ll.setLayoutParams(rlp);
WebView wv = makeWebView();
//wv.setBackgroundColor(0);
//wv.setBackgroundResource(R.color.Blue);
//wv.setBackgroundResource(Color.BLUE);
wv.loadData("<html><body>TEST</body></html>", "text/html", "utf-8");
RelativeLayout rl = (RelativeLayout)findViewById(R.id.RR1);
//rl.addView(wv);
//ll.addView(wv);
}
});
}
protected WebView makeWebView() {
return new WebView(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
我调整了线性布局的大小并将其移动了,因此需要将Webview放置在RR1中的某个位置,而不是我收集的内容。我正在查看各种文档,它或者不是通过随机性寻找或接受的,我只是没有把握它。
您是否了解如何实现这一目标?
作为旁注:我不知道为什么onCreateOptionMenu方法存在。我不打算使用它,并且会最终删除它。
答案 0 :(得分:4)
试试rl.addView(wv, 0);
会做的伎俩:)