创建重叠的textview和webview

Shw*_*eta 0 android textview webview

我有一个应用程序布局,其中我使用webview作为基础资产文件夹包含HTML文件,一切都很好.我希望在活动启动时默认在webview的同一位置获得动态文本视图,但是当用户单击按钮时我想删除文本视图并将其替换为webview.谁能帮我.试过的方法: - 我在按钮上做了两个相似的布局和用户响应,然后用webview打开相同的布局活动.这是漫长的方式,但我可以管理.

Bir*_*dia 5

这样做

1)创建布局"activity_main.xml"

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:id="@+id/btnChange"
        android:text="Show Webview"
        android:layout_gravity="right|center_vertical"/>

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" >

        <WebView
            android:id="@+id/webview"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:visibility="gone" />

        <TextView
            android:id="@+id/textView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="This is Textview.This is Textview.This is Textview.This is Textview.This is Textview.This is Textview.This is Textview.This is Textview.This is Textview." />
    </FrameLayout>

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

2)创建Java"MainActivity.java"

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {

    private WebView webview;
    private TextView textView;
    private Button btnChange;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        webview = (WebView) findViewById(R.id.webview);
        webview.setWebViewClient(new WebViewClient());
        webview.loadUrl("http://www.google.com");
        textView = (TextView) findViewById(R.id.textView);
        btnChange = (Button) findViewById(R.id.btnChange);

        btnChange.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

                if (webview.getVisibility() == View.VISIBLE) {
                    webview.setVisibility(View.GONE);
                    textView.setVisibility(View.VISIBLE);
                    btnChange.setText("Show WebView");
                } else {
                    webview.setVisibility(View.VISIBLE);
                    textView.setVisibility(View.GONE);
                    btnChange.setText("Show TextView");
                }

            }
        });
    }

}
Run Code Online (Sandbox Code Playgroud)

输出:

在此输入图像描述

在此输入图像描述