【发布时间】:2012-12-11 06:47:18
【问题描述】:
我有一个附加了点击侦听器的表面视图。
当我单击它时,它会将其缩小为宽度的一半和高度的一半...并通过以下方式将其放置在右下角:
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 方法。我不打算使用它,并且最终会删除它。
【问题讨论】:
-
为什么不尝试在 xml 中添加 WebView 而不是 SurfaceView。必须有一个 id 将您的 xml 与 java 代码链接起来。 developer.android.com/guide/webapps/webview.html通过这个
-
如果您愿意,您可以通过编程方式在 Android 中制作所有内容。我应该能够将 childNode 添加到 ParentNode 没问题。我说的对吗
-
是的,但这是一个糟糕的方法。如果您以编程方式完成所有操作,那么您将无法支持所有屏幕。因此,将所有与 UI 相关的内容放在工作区中的 XML 和 java 代码中。
-
我知道将 UI 和后端分开是明智的,但这个示例不能做到这一点。我不是在争论这一点。我只是好奇怎么做。
-
你试过'rl.addView(wv, 0);'吗??
标签: java android user-interface view