【问题标题】:Group More Than One Controls At Android在 Android 上对多个控件进行分组
【发布时间】:2012-04-06 08:38:33
【问题描述】:

我创建了一个带有 RelativeLayout 的窗口,并在其中插入了两个控件;一个是GridView,一个是WebView。

我会将图像绑定到 gridview 以使用类似菜单,并且我将在 webview 中加载一个用于广告的 html 页面。

但我对这些控件有疑问。 例如,如果用户垂直滚动 gridview,它会消失在 webview 后面,因为我期待 webview 在用户滚动 gridview 时也会与 gridview 一起滑动。

所以我想知道,我可以将这些控件组合在一起以同时执行吗?

我的另一个方向问题,当我将我的设备转到横向模式时,webview 正在从屏幕上消失。

最后,如果我设置了 GridView 的 layout_height 属性 fill_parent,我看不到 WebView,所以我将它设置为 wrap_content。但是这次WebView没有底部对齐,它在GridView的后面对齐并且屏幕底部和webview之间有空间。

     <RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:background="@drawable/bg480480">

     <GridView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/gridview"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
    android:numColumns="auto_fit"
    android:verticalSpacing="45dp"
    android:horizontalSpacing="10dp"
    android:columnWidth="150dp"
    android:stretchMode="columnWidth"
    android:gravity="center"
    android:layout_marginTop="30dp"
    android:scrollbars="none"
    android:layout_alignParentTop="true"   />

        <WebView
        android:id="@+id/webView1"
        android:layout_width="wrap_content"      
        android:layout_height="wrap_content"
        android:scaleType="centerInside"
        android:gravity="bottom"
        android:layout_below="@id/gridview"   />

</RelativeLayout>

这里是一些屏幕截图。

【问题讨论】:

  • 尝试将您的 RelativeLayout 放入 ScrollView。

标签: android android-layout


【解决方案1】:

我猜主要问题是 GridView 和 Webview 都指定了layout_height="fill_parent"。我猜你可能不希望 GridView 填满整个父区域,所以请改用"wrap_content"

我对所需的页面布局知之甚少,但我建议使用 LinearLayout 开始并获得舒适感。这实际上取决于网格视图和 Web 视图应该显示什么以及您的主要内容在哪里。

在下面的示例中,我只是假设您的 Web 视图想要占据屏幕的大部分。

     <GridView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/gridview"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
    android:background="#ff0000"
    android:numColumns="auto_fit"
    android:verticalSpacing="45dp"
    android:horizontalSpacing="10dp"
    android:columnWidth="150dp"
    android:stretchMode="columnWidth"
    android:layout_marginTop="30dp"
    android:scrollbars="none"
    />

        <WebView
            android:id="@+id/webView1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#00ff00"
            android:scaleType="centerInside" />

</LinearLayout>

如果假设不正确,那么您可能会反过来指定它 (Webview=>wrap_conent)

【讨论】:

  • 感谢您的回答,但此解决方案未显示 webview。
【解决方案2】:

改进WebView布局代码如下:

<GridView
    android:id="@+id/gridview"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_above="@+id/webView1"
    android:layout_alignParentTop="true"
    android:layout_marginTop="30dp"
    android:columnWidth="150dp"
    android:gravity="center"
    android:horizontalSpacing="10dp"
    android:numColumns="auto_fit"
    android:scrollbars="none"
    android:stretchMode="columnWidth"
    android:verticalSpacing="45dp" />

<WebView
    android:id="@+id/webView1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:gravity="bottom"
    android:scaleType="centerInside" />

仅供参考,android:layout_alignParentBottom="true" 将在屏幕底部显示 WebView。并在 GridView 中包含 android:layout_above="@+id/webView1"

试试这个布局代码。

【讨论】:

  • 我添加了当前情况的截图。如您所见,它非常奇怪。
【解决方案3】:

您需要添加 android:layout_above="@+id/webView1" 在您的网格视图中 还 android:layout_alignParentBottom="true" 在您的网络视图中

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:background="@drawable/bg480480"
    android:orientation="vertical"
    >

     <GridView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/gridview"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
    android:numColumns="auto_fit"
    android:verticalSpacing="45dp"
    android:horizontalSpacing="10dp"
    android:columnWidth="150dp"
    android:stretchMode="columnWidth"
    android:gravity="center"
    android:layout_above="@+id/webView1"
    android:layout_marginTop="30dp"
    android:scrollbars="none"
    android:layout_alignParentTop="true"   />

        <WebView
             android:id="@+id/webView1"
             android:layout_width="wrap_content"      
             android:layout_height="wrap_content"
             android:layout_alignParentBottom="true"
             android:layout_below="@id/gridview"   />

</RelativeLayout>

【讨论】:

    猜你喜欢
    • 2019-03-28
    • 1970-01-01
    • 1970-01-01
    • 2021-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-02
    • 2012-04-28
    相关资源
    最近更新 更多