【问题标题】:Android WebView but no other componentsAndroid WebView 但没有其他组件
【发布时间】:2017-03-03 13:14:49
【问题描述】:

正在寻找一个在侧面按钮旁边有 webviewTextviews 的网络。在代码下方运行时,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>

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

【问题讨论】:

标签: android xml webview


【解决方案1】:

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

考虑以下几点:

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

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

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

如果您希望 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>

【讨论】:

  • 谢谢。您是说必须在同一活动上进行布局。我还减小了 webview 的大小,没有任何区别。
  • @MarkJames 不要在 WebView 上使用“fill_parent”的高度,而是使用我提供的代码。将 WebView 的权重设为 1,而使用 wrap_content 的高度将自动占用剩余空间。
  • 尝试复制和粘贴完全相同的代码;您的布局高度应设置为“wrap_content”,其他元素的权重应为 0。
猜你喜欢
  • 2021-12-02
  • 2013-09-12
  • 2018-12-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-04
  • 2022-06-15
  • 1970-01-01
相关资源
最近更新 更多