ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

9.1借助WebView控件在应用程序中嵌入一个百度浏览器

2021-12-09 23:02:16  阅读:181  来源: 互联网

标签:comment 控件 String title 9.1 content import WebView public


1) activity_main:

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="horizontal">
    <WebView
        android:id="@+id/wb"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </WebView>
    <FrameLayout
        android:layout_height="match_parent"
        android:layout_width="match_parent">

    <LinearLayout
        android:orientation="vertical"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:visibility="invisible"
        android:gravity="center"
        android:id="@+id/loading">
    <ProgressBar
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"/>

    <TextView android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="正在加载信息..."/>
</LinearLayout>
    <ListView
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:id="@+id/lv_news"/>

</FrameLayout>
</LinearLayout>

2) news_item:

 

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="65dp">
    <RelativeLayout
        android:id="@+id/siv_icon"
        android:layout_width="80dp"
        android:layout_height="60dp"
        android:layout_alignParentLeft="true"
        android:layout_marginBottom="5dp"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="5dp"
        android:scaleType="centerCrop"
        android:src="@mipmap/ic_launcher">
    </RelativeLayout>
    <TextView
        android:id="@+id/tv_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="10dp"
        android:layout_toRightOf="@id/siv_icon"
        android:ellipsize="end"
        android:maxLength="20"
        android:singleLine="true"
        android:text="我是标题"
        android:textColor="#000000"
        android:textSize="18sp" />
    <TextView
        android:id="@+id/tv_description"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/tv_title"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="5dp"
        android:layout_toRightOf="@id/siv_icon"
        android:ellipsize="end"
        android:maxLength="16"
        android:singleLine="true"
        android:text="我是描述"
        android:textColor="#99000000"
        android:textSize="14sp" />
    <TextView
        android:id="@+id/tv_type"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_marginBottom="5dp"
        android:layout_marginRight="10dp"
        android:text="评论"
        android:textColor="#99000000"
        android:textSize="12sp" />
</RelativeLayout>

3) MainActivity:

package com.example.chap9;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ListView;

public class MainActivity extends AppCompatActivity {

    private WebView wb;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         wb= (WebView) findViewById(R.id.wb);
         //让webview控件支持js脚本
        wb.getSettings().setJavaScriptEnabled(true);
        //当需要从一个网页跳转到另外一个网页时,
        // 目标网页仍然在wbview控件中显示,而不是打开浏览器
        wb.setWebViewClient(new WebViewClient());
        wb.loadUrl("http://www.baidu.com");


    }

}

4) JsonParse:

package com.example.chap9;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.List;

//解析json数据的工具类
public class JsonParse {
    public static List<NewsInfo> getJson(String json){
        Gson gson=new Gson();
        Type listType=new TypeToken<List<NewsInfo>>(){}.getType();//泛型
       List<NewsInfo> list=gson.fromJson(json,listType);

    return null;
    }
}

5)NesInfo:

package com.example.chap9;

public class NewsInfo {
    private String icon;
    private String title;
    private String content;
    private String type;
    private String comment;
    public NewsInfo(String icon, String title, String content, String type, String comment) {
        this.icon = icon;
        this.title = title;
        this.content = content;
        this.type = type;
        this.comment = comment;
    }
    public String getIcon() {
        return icon;
    }
    public void setIcon(String icon) {
        this.icon = icon;
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getContent() {
        return content;
    }
    public void setContent(String content) {
        this.content = content;
    }
    public String getType() {
        return type;
    }
    public void setType(String type) {
        this.type = type;
    }
    public String getComment() {
        return comment;
    }
    public void setComment(String comment) {
        this.comment = comment;
    }
}

6) AndroidManifest:

 

 

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.chap9">
   <!-- 申请权限-->
    <uses-permission android:name="android.permission.INTERNET"></uses-permission>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.Chap9">
        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

 

 

 

 

 

标签:comment,控件,String,title,9.1,content,import,WebView,public
来源: https://blog.csdn.net/qq_54152228/article/details/121845587

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有