webview 在选项卡中无法正确显示
我在水平滚动视图中设置了几个选项卡,我试图显示所选选项卡的活动的 Web 视图,但 Web 视图不是屏幕的宽度,而是显示选项卡视图的宽度,基本上否定了我的内容我正在尝试使用滚动视图中的选项卡来完成。
如果我将一个 webview 与选项卡嵌套在一起,它将显示我想要的方式,但这只是显示一个 webview 而不是启动实际的活动
选项卡 xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_height="fill_parent" android:layout_width="fill_parent">
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="vertical"
android:layout_height="fill_parent" android:layout_width="fill_parent">
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost" android:layout_width="750dp"
android:layout_height="fill_parent">
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<View android:layout_width="fill_parent" android:layout_height="0.5dip"
android:background="#000" />
<TabWidget android:id="@android:id/tabs"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_marginLeft="0dip" android:layout_marginRight="0dip" />
<View android:layout_width="fill_parent" android:layout_height="2dip"
android:background="#696969" />
<View android:layout_width="fill_parent" android:layout_height="2dip"
android:background="#000" />
<FrameLayout android:id="@android:id/tabcontent"
android:layout_width="fill_parent" android:layout_height="fill_parent" />
</LinearLayout>
</TabHost>
</LinearLayout>
</HorizontalScrollView>
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/webView2"
android:layout_height="fill_parent"
android:layout_width="wrap_content"></WebView>
</LinearLayout>
tabactivity
public class Tabs extends TabActivity {
private TabHost mTabHost;
private void setupTabHost() {
mTabHost = (TabHost) findViewById(android.R.id.tabhost);
mTabHost.setup();
}
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setupTabHost();
mTabHost.getTabWidget().setDividerDrawable(R.drawable.tab_divider);
Intent intent;
intent = new Intent().setClass(this, Main.class);
setupTab(new TextView(this), "Tab 1",intent);
intent = new Intent().setClass(this, SmsMain.class);
setupTab(new TextView(this), "Tab 2",intent);
intent = new Intent().setClass(this, BatteryMain.class);
setupTab(new TextView(this), "Tab 3",intent);
intent = new Intent().setClass(this, MailMain.class);
setupTab(new TextView(this), "Tab 4",intent);
WebView browser = (WebView) findViewById(R.id.webView2); //this way displays fine but its not starting an activity
browser.loadUrl("file:///android_asset/about.html");
}
private void setupTab(final View view, final String tag, Intent intent) {
View tabview = createTabView(mTabHost.getContext(), tag);
TabSpec setContent = mTabHost.newTabSpec(tag).setIndicator(tabview).setContent(intent);
mTabHost.addTab(setContent);
}
private static View createTabView(final Context context, final String text) {
View view = LayoutInflater.from(context).inflate(R.layout.tabs_bg, null);
TextView tv = (TextView) view.findViewById(R.id.tabsText);
tv.setText(text);
return view;
}
}
和带有 webview 的活动
public class Main extends Activity {
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main_webview);
Log.d("Debug", "main oncreate");
WebView browser = (WebView)findViewById(R.id.webView1); //displays the whole tabview
//setContentView(browser);
browser.loadUrl("file:///android_asset/about.html");
}
基本上我唯一想要水平滚动的是选项卡,我想正常显示的其他所有内容
可以完成我想做的事情吗?
I have a few tabs set up in a horizontal scroll view and i am trying to display a webview of an activity of the tab selected but instead of the webview being the width of the screen it displays the width of the tabview, basically negating what i am trying to accomplish with the tabs in a scroll view.
If I nest a webview in with the tabs it will display how i want but that would be just showing a webview and not starting an actual activity
tabs xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_height="fill_parent" android:layout_width="fill_parent">
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="vertical"
android:layout_height="fill_parent" android:layout_width="fill_parent">
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost" android:layout_width="750dp"
android:layout_height="fill_parent">
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<View android:layout_width="fill_parent" android:layout_height="0.5dip"
android:background="#000" />
<TabWidget android:id="@android:id/tabs"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_marginLeft="0dip" android:layout_marginRight="0dip" />
<View android:layout_width="fill_parent" android:layout_height="2dip"
android:background="#696969" />
<View android:layout_width="fill_parent" android:layout_height="2dip"
android:background="#000" />
<FrameLayout android:id="@android:id/tabcontent"
android:layout_width="fill_parent" android:layout_height="fill_parent" />
</LinearLayout>
</TabHost>
</LinearLayout>
</HorizontalScrollView>
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/webView2"
android:layout_height="fill_parent"
android:layout_width="wrap_content"></WebView>
</LinearLayout>
tabactivity
public class Tabs extends TabActivity {
private TabHost mTabHost;
private void setupTabHost() {
mTabHost = (TabHost) findViewById(android.R.id.tabhost);
mTabHost.setup();
}
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setupTabHost();
mTabHost.getTabWidget().setDividerDrawable(R.drawable.tab_divider);
Intent intent;
intent = new Intent().setClass(this, Main.class);
setupTab(new TextView(this), "Tab 1",intent);
intent = new Intent().setClass(this, SmsMain.class);
setupTab(new TextView(this), "Tab 2",intent);
intent = new Intent().setClass(this, BatteryMain.class);
setupTab(new TextView(this), "Tab 3",intent);
intent = new Intent().setClass(this, MailMain.class);
setupTab(new TextView(this), "Tab 4",intent);
WebView browser = (WebView) findViewById(R.id.webView2); //this way displays fine but its not starting an activity
browser.loadUrl("file:///android_asset/about.html");
}
private void setupTab(final View view, final String tag, Intent intent) {
View tabview = createTabView(mTabHost.getContext(), tag);
TabSpec setContent = mTabHost.newTabSpec(tag).setIndicator(tabview).setContent(intent);
mTabHost.addTab(setContent);
}
private static View createTabView(final Context context, final String text) {
View view = LayoutInflater.from(context).inflate(R.layout.tabs_bg, null);
TextView tv = (TextView) view.findViewById(R.id.tabsText);
tv.setText(text);
return view;
}
}
and activity with a webview
public class Main extends Activity {
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main_webview);
Log.d("Debug", "main oncreate");
WebView browser = (WebView)findViewById(R.id.webView1); //displays the whole tabview
//setContentView(browser);
browser.loadUrl("file:///android_asset/about.html");
}
basically the only thing i want to be horizontally scrolling is the tabs, everything else i want to display normally
can what i am trying to do be done?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
(对之前的答案感到抱歉,读得太快了)
您的
HorizontalScrollView
的layout_width
和您的WebView
的layout_width 均设置为
wrap_content
。wrap_content
值对于可以朝该方向滚动的内容来说效果不是特别好。假设此布局中没有其他内容,请尝试将这两个layout_width
切换为fill_parent
(或match_parent
)。(sorry about previous answer, was reading your stuff too quickly)
Your
HorizontalScrollView
'slayout_width
and yourWebView
'slayout_width
are both set towrap_content
. Thewrap_content
value does not work especially well with things that can scroll in that direction. Assuming there is nothing else in this layout, try switching both of thoselayout_width
to befill_parent
(ormatch_parent
).