你可以聲明自定義視圖並按照here所述應用自定義屬性。
結果將類似於此:
在佈局
<my.package.CustomWebView
custom:url="@string/myurl"
android:layout_height="match_parent"
android:layout_width="match_parent"/>
在attr.xml
<resources>
<declare-styleable name="Custom">
<attr name="url" format="string" />
</declare-styleable>
</resources>
終於在自定義的Web視圖類
public class CustomWebView extends WebView {
public CustomWebView(Context context, AttributeSet attributeSet) {
super(context);
TypedArray attributes = context.getTheme().obtainStyledAttributes(
attributeSet,
R.styleable.Custom,
0, 0);
try {
if (!attributes.hasValue(R.styleable.Custom_url)) {
throw new RuntimeException("attribute myurl is not defined");
}
String url = attributes.getString(R.styleable.Custom_url);
this.loadUrl(url);
} finally {
attributes.recycle();
}
}
}
注意名稱_layout_ xml文件。我不認爲有任何方法可以從XML做到這一點。我明白了你的觀點,你希望能夠在設備上加載數據而無需從代碼加載url,但我認爲現在沒有任何支持。 – 2011-03-07 12:53:05
Cant0na看到我的觀點的目標! – 2011-03-07 13:06:43