Android:并排放置2个Web浏览器

时间:2014-08-26 07:22:42

标签: android webview

我有一个LinearLayout后跟一个Button,然后是2个webview,然后是RelativeLayout中的另一个LinearLayout。网络视图需要并排。您可以想象它看起来像http://developer.android.com/training/左手菜单和右手内容,顶部菜单和底部页脚。

单击该按钮时,将加载Web视图。但是,这不起作用。只有一个webview正在加载内容。我注意到如果我从webview中删除“layout_toRightOf”并在另一个下方显示webview,那么它工作正常。或者,如果我删除“layout_toRightOf”并使用“layout_toLeftOf”作为其他webview。然后是laer webview显示内容。请告诉我,我怎么能做到这一点。

我也试过Table Layout但没有运气。我尝试过wrap_content或固定宽度,但也没有运气。

此外,请告知我如何通过点击图标或其他内容隐藏左侧网页视图。发生这种情况时,正确的webview应该拉伸到全宽。我已将我的代码包含在内以供参考。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="@dimen/menu_height"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".HomeActivity">

<LinearLayout
    android:id="@+id/llayoutTop"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true">

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="some text on top edit" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="button top" />
</LinearLayout>


<Button
    android:id="@+id/home_bGetFile"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/llayoutTop"
    android:layout_gravity="bottom"
    android:onClick="getFiles"
    android:text="@string/button_getFiles" />    

<WebView
    android:id="@+id/home_webViewLeft"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/home_bGetFile" />

<WebView
    android:id="@+id/home_webViewRight"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/home_bGetFile"
    android:layout_toRightOf="@id/home_webViewLeft" />

<LinearLayout
    android:id="@+id/llayoutBottom"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true">

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="some text on bottom edit" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="button bottom" />
</LinearLayout>

2 个答案:

答案 0 :(得分:1)

试试这个。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="@dimen/menu_height"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".HomeActivity" >

    <LinearLayout
        android:id="@+id/llayoutTop"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true" >

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:hint="some text on top edit" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="button top" />
    </LinearLayout>

    <Button
        android:id="@+id/home_bGetFile"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/llayoutTop"
        android:layout_gravity="bottom"
        android:onClick="getFiles"
        android:text="@string/button_getFiles" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/llayoutBottom"
        android:layout_below="@+id/home_bGetFile" >

        <WebView
            android:id="@+id/home_webViewLeft"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1" />

        <WebView
            android:id="@+id/home_webViewRight"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/llayoutBottom"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true" >

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:hint="some text on bottom edit" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="button bottom" />
    </LinearLayout>

</RelativeLayout>

答案 1 :(得分:0)

WebViews放入LinearLayout,然后将width设置为0,将weight设置为1。

<LinearLayout
    android:layout_width="match_parent"
    android:height="wrap_content"
    android:orientation="horizontal"
    android:layout_below="@id/home_bGetFile">

<WebView
    android:id="@+id/home_webViewLeft"
    android:layout_width="0"
    android:layout_height="wrap_content"
    android:layout_weight="0"/>

<WebView
    android:id="@+id/home_webViewRight"
    android:layout_width="0"
    android:layout_height="wrap_content"
    android:layout_weight="0"/>
</LinearLayout>

你正在做的事情不起作用,是因为第一个WebView将宽度设置为"wrap_content",这可能导致它占据整个屏幕,然后你'试图告诉另一个视图坐在右侧(屏幕外)。 RelativeLayouts就是这样 - 他们的立场是相对的。因此,除非您将宽度设置为屏幕宽度的一半(难以做到),否则最好使用LinearLayout