Android WebView 但没有其他组件

标签 android xml webview

正在寻找具有 webview 以及侧边按钮和 Textviews 的网络。在代码下方运行时,webview 是该 Activity 上唯一可用的内容。

public class Tab2Activity extends Activity
{
    private WebView xWebview ;



    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tab2);


        ///////////////////////////////////////////////

        TextView tvVenue = (TextView)findViewById(R.id.venue);
        TextView tvAddress = (TextView)findViewById(R.id.address);
        TextView tvCity = (TextView)findViewById(R.id.city);
        TextView tvPostCode = (TextView)findViewById(R.id.postcode);
        TextView tvDate = (TextView)findViewById(R.id.Vdate);
        TextView tvTime = (TextView)findViewById(R.id.Vtime);




        Button fest = (Button)findViewById(R.id.veganfestival);
        Button nav = (Button)findViewById(R.id.nav);
        Button buyTicket = (Button)findViewById(R.id.buyticket);

        fest.setText(MainActivity.whichFest);

        if (MainActivity.whichFest.equals("Aberdeen"))
        {
            tvVenue.setText("Aberdeen Football Club");
            tvAddress.setText("Pittodrie Street");
            tvCity.setText("Aberdeen");
            tvPostCode.setText("AB24 5QH");
            tvDate.setText("Saturday 28th May 2017");
            tvTime.setText("10:30am - 4pm");
            venueLocation="Aberdeen Football Club";

        }
        //////////////////////////////////////////////
        if (MainActivity.whichFest.equals("Newcastle"))
        {
            tvVenue.setText("The Assembly Rooms");
            tvAddress.setText("Fenkle Street");


        xWebview = new WebView(this);

        xWebview.getSettings().setJavaScriptEnabled(true); // enable javascript


        xWebview.setWebChromeClient(new WebChromeClient());
        xWebview.clearCache(true);
        xWebview.getSettings().setUserAgentString("Android WebView");
        xWebview.loadUrl(MainActivity.MapLocation);
        setContentView(xWebview);
}

这是 XML

<ScrollView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools"
    android:gravity="center"
    android:background="#332b2b"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:id="@+id/linearlayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"

        android:orientation="vertical" >

        <Button
            android:id="@+id/veganfestival"

            android:text="Aberdeen"
            android:textColor="#FFFFFF"
            android:textSize="30sp"

            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:background="@drawable/buttonshape"
            android:shadowColor="#A8A8A8"
            android:shadowDx="0"
            android:shadowDy="0"
            android:shadowRadius="5"

            />
        <TextView
            android:id="@+id/blank"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"

            android:textSize="20dp"
            android:textColor="#ffffff" />
        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="#6f844a" />
        <TextView
            android:id="@+id/venue"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Venue"
            android:textSize="20dp"
            android:textColor="#ffffff" />

        <TextView
            android:id="@+id/address"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Address"
            android:textSize="20dp"
            android:textColor="#ffffff" />
        <TextView
            android:id="@+id/city"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="City"
            android:textSize="20dp"
            android:textColor="#ffffff" />
        <TextView
            android:id="@+id/postcode"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Postcode"
            android:textSize="20dp"
            android:textColor="#ffffff" />
        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="#6f844a" />
        <TextView
            android:id="@+id/Vdate"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Date"
            android:textSize="20dp"
            android:textColor="#ffffff" />
        <TextView
            android:id="@+id/Vtime"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Time"
            android:textSize="20dp"
            android:textColor="#ffffff" />
        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="#6f844a" />

        <TextView
            android:id="@+id/blankagain"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"

            android:textSize="20dp"
            android:textColor="#ffffff" />

        <Button
            android:id="@+id/buyticket"

            android:text="Buy Ticket"
            android:textColor="#FFFFFF"
            android:textSize="30sp"

            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:background="@drawable/buttonshape"
            android:shadowColor="#A8A8A8"
            android:shadowDx="0"
            android:shadowDy="0"
            android:shadowRadius="5"

            />
        <TextView
            android:id="@+id/blankmore"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"

            android:textSize="20dp"
            android:textColor="#ffffff" />

        <WebView
            android:id="@+id/webviewb"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            />

        <Button
            android:id="@+id/nav"

            android:text="Take Me There"
            android:textColor="#FFFFFF"
            android:textSize="30sp"

            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:background="@drawable/buttonshape"
            android:shadowColor="#A8A8A8"
            android:shadowDx="0"
            android:shadowDy="0"
            android:shadowRadius="5"

            />


    </LinearLayout>
    </ScrollView>

如果有人有解决方法或关于如何实现此目标的建议 - 将不胜感激。

最佳答案

问题显然与 XML 有关,因为 webview 占用了所有可用空间。

考虑以下因素:

  • WebView 的宽度和高度为“fill_parent”,这意味着它的高度完全与其包裹的 LinearLayout 相同。

  • 您的 TextView 和 Button 也包含在同一个 LinearLayout 中,不允许它们可见,因为 webview 占用了所有空间。

我建议将 webview 移动到不同的布局中(它可以是线性布局或相对布局)。根据 View 的需要,您可以使用权重、dps 或仅通过包装内容来定义 TextView 和按钮使用的空间。

如果您希望 webview 使用所有“剩余空间”,您可以使用 LinearLayout,并在这种情况下为您的 WebView 赋予 1 的权重。像这样:

<LinearLayout 
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:orientation="horizontal"
      >

     <Button
        android:layout_width = "match_parent"
        android:layout_weight = "0"
        android:layout_height = "wrap_content"
        android:text="text!"/>
     <WebView
        android:layout_width = "fill_parent"
        android:layout_height = "wrap_content"
        android:layout_weight = "1"/>
     <Button
        android:layout_width = "80dp"
        android:layout_weight = "0"
        android:layout_height = "wrap_content"
        android:text="text!"/>   
 </LinearLayout>

关于Android WebView 但没有其他组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42579876/

相关文章:

java - 解码异常 : unexpected element on XML just been Marshaller

android - 通过 setInputType() 设置输入类型后,自定义 EditText 不起作用

c# - 将 2 个 XML 文件合并到相同的元素中 C#

android - 如何解决这个问题 “signed apk is not generating when minifyEnabled true in app gradle ”

javascript - 我可以使用 xslt 来显示下拉菜单吗

java - Android Phonegap 2.1 > 2.2 升级错误

html - 使用 css 在页面结果样式上更改 webview 查找?

java - webview 不加载我的 html

android - ViewModel 中的 LiveData 如何使用 Transformations 观察 Repository 中的 LiveData?

java - 最低android API级别