webview 在选项卡中无法正确显示

发布于 2024-10-31 02:38:31 字数 4255 浏览 0 评论 0原文

我在水平滚动视图中设置了几个选项卡,我试图显示所选选项卡的活动的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱你是孤单的心事 2024-11-07 02:38:31

(对之前的答案感到抱歉,读得太快了)

您的 Horizo​​ntalScrollViewlayout_width 和您的 WebViewlayout_width 均设置为 wrap_contentwrap_content 值对于可以朝该方向滚动的内容来说效果不是特别好。假设此布局中没有其他内容,请尝试将这两个 layout_width 切换为 fill_parent (或 match_parent)。

(sorry about previous answer, was reading your stuff too quickly)

Your HorizontalScrollView's layout_width and your WebView's layout_width are both set to wrap_content. The wrap_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 those layout_width to be fill_parent (or match_parent).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文