【问题标题】:Programmatically displaying a WebView with a TextView below it以编程方式显示 WebView,其下方带有 TextView
【发布时间】:2012-07-01 10:02:15
【问题描述】:

以下 XML 实现似乎工作正常:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">

    <WebView
        android:id="@+id/chatView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1" />

    <EditText
        android:id="@+id/text"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
    </EditText>
</LinearLayout>

但是,下面的 Java 实现没有(WebView 后面的底部有一个很小的空间,但 TextView 不可见。)

Context mContext = getActivity();

LinearLayout view = new LinearLayout(mContext);
view.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
view.setOrientation(LinearLayout.VERTICAL);

WebView web (new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, 1f));
web.loadUrl("http://www.google.com");

TextView text = new TextView(mContext);
text.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

view.addView(web);
view.addView(text);

例子:

TextView 应该是底部黑色空间的位置,但当然更高。任何帮助将不胜感激,谢谢。

【问题讨论】:

    标签: android android-layout webview textview


    【解决方案1】:

    如果您尝试使WebView 围绕TextView 调整大小(我假设这是您想要的,因为您拥有android:weight 属性),请确保将高度设置为“@ 987654324@" 而不是 "fill_parent"。否则,WebView 将填充父级,而您的 TextView 将不会显示。

    此外,由于TextView 的高度设置为“wrap_content”,如果您想看到它,您实际上需要那里的内容。设置文本后查看它是否显示。

    【讨论】:

    • 做到了!感谢您及时的回复。我还打算使用 EditText 而不是 TextView,呵呵。这总是一些愚蠢的错误。
    【解决方案2】:

    我认为你必须在 TextView "text" 上设置一些文本

    类似这样text.setText("This is text ")

    【讨论】:

      猜你喜欢
      • 2011-05-22
      • 2014-12-11
      • 1970-01-01
      • 1970-01-01
      • 2022-01-21
      • 2017-07-09
      • 1970-01-01
      • 1970-01-01
      • 2011-09-05
      相关资源
      最近更新 更多